tournasdim
5/31/2014 - 9:01 AM

A simple web-app build on top of Symfony's Request,Response,HttpKernel,Route,RouteCollection components

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(); 
	}