PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Create your own framework... on top of the Symfony2 Components (part 12)

Note: This article was originally published at Planet PHP on 16 April 3540.
Planet PHP

This article is part of a series of articles that explains how to create a framework with the Symfony2 Components: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

In the last installment of this series, we have emptied the Simplex\\Framework class by extending the HttpKernel class from Symfony. Seeing this empty class, you might be tempted to move some code from the front controller to it:

A // example.com/src/Simplex/Framework.php A namespace Simplex; A use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\Routing; use Symfony\Component\HttpKernel; use Symfony\Component\EventDispatcher\EventDispatcher; A class Framework extends HttpKernel { public function __construct($routes) { $context = new Routing\RequestContext(); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); $resolver = new HttpKernel\Controller\ControllerResolver(); A $dispatcher = new EventDispatcher(); $dispatcher-addSubscriber(new HttpKernel\EventListener\RouterListener($matcher)); $dispatcher-addSubscriber(new HttpKernel\EventListener\ResponseListener('UTF-8')); A parent::__construct($dispatcher, $resolver); } } A

The front controller code would become more concise:

A // example.com/web/front.php A require_once __DIR__.'/../vendor/.composer/autoload.php'; A use Symfony\Component\HttpFoundation\Request; A $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/app.php'; A $framework = new Simplex\Framework($routes); A

Truncated by Planet PHP, read more at the original (another 18437 bytes)