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:
<?php // example.com/src/Simplex/Framework.php namespace Simplex; use Symfony\Component [HttpKernel\HttpKernel;] use Symfony\Component\Routing; use Symfony\Component [HttpKernel;] use Symfony\Component\EventDispatcher\EventDispatcher; class Framework extends [HttpKernel] { public function __construct($routes) { $context = new Routing\RequestContext(); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); $resolver = new [HttpKernel\Controller\ControllerResolver();] $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new [HttpKernel\EventListener\RouterListener(] matcher)); $dispatcher->addSubscriber(new [HttpKernel\EventListener\ResponseListener('UTF-8'));] parent::__construct($dispatcher, $resolver); } }
The front controller code would become more concise:
<?php // example.com/web/front.php require_once __DIR__.'/../vendor/.composer/autoload.php'; use Symfony\Component [HttpFoundation\Request;] $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/app.php'; $framework = new Simplex\Framework($routes);
Truncated by Planet PHP, read more at the original (another 18437 bytes)