Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created May 31, 2014 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tournasdim/e7ed10515d924522c5f9 to your computer and use it in GitHub Desktop.
Save tournasdim/e7ed10515d924522c5f9 to your computer and use it in GitHub Desktop.
A simple web-app build on top of Symfony's Request,Response,HttpKernel,Route,RouteCollection components
<?php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
require 'vendor/autoload.php';
$routes = new RouteCollection();
$routes->add('foo' , new Route('/foo' , [
'_controller' => function (Request $request) {
return new Response('foo!!!!');
}]));
$routes->add('hello' , new Route('/hello/{name}' , [
'_controller' => function (Request $request , $name ) {
return new Response('Hello'. $name);
}]));
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new RouterListener(
new UrlMatcher($routes , new RequestContext())));
$kernel = new HttpKernel($dispatcher, new ControllerResolver());
$request = Request::createFromGlobals();
try
{
$response = $kernel->handle($request);
$response->send();
}
catch (NotFoundHttpException $e)
{
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment