Last active
April 20, 2016 14:50
-
-
Save 1ed/166a35db46b6bb7c7166e99049a9ad0a to your computer and use it in GitHub Desktop.
Using Closures as controllers in Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\EventListener; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
class AppControllerListener | |
{ | |
public function onKernelRequest(GetResponseEvent $event) | |
{ | |
$request = $event->getRequest(); | |
$route = $request->attributes->get('_route'); | |
$routeDefinitions = include dirname(__DIR__).'/routes.php'; | |
if (isset($routeDefinitions[$route])) { | |
$request->attributes->set('_controller', $routeDefinitions[$route]['controller']); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpFoundation\Response; | |
return [ | |
'app_default_index' => [ | |
'path' => '/', | |
'controller' => function () { | |
return new Response('A Closure Controller');; | |
}, | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Config\Resource\FileResource; | |
use Symfony\Component\Routing\RouteCollection; | |
use Symfony\Component\Routing\Route; | |
/** @var \Symfony\Component\Routing\Loader\PhpFileLoader $loader */ | |
$path = $loader->getLocator()->locate('@AppBundle/routes.php'); | |
$routeDefinitions = include $path; | |
$collection = new RouteCollection(); | |
$collection->addResource(new FileResource($path)); | |
foreach ($routeDefinitions as $name => $definition) { | |
$collection->add($name, new Route($definition['path'])); | |
} | |
return $collection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_closure: | |
resource: "@AppBundle/Resources/config/routing.php" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.event_listener.controller: | |
class: AppBundle\EventListener\ControllerListener | |
tags: | |
- { name: 'kernel.event_listener', event: 'kernel.request', method: 'onKernelRequest', priority: 31 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or an other solution would be to use RouteCollectionBuilder to load the routes like it's done in the MicroKernel.