Created
April 15, 2015 10:29
-
-
Save rossriley/977339ed752337173819 to your computer and use it in GitHub Desktop.
Use DI Managed Controllers in Bolt
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 | |
// in Myapp/Provider/ControllerProvider.php | |
namespace Myapp\Provider; | |
use Silex\ServiceProviderInterface; | |
use Silex\Application; | |
use Myapp\Routing\ControllerResolver | |
/** | |
* | |
*/ | |
class ControllerProvider implements ServiceProviderInterface | |
{ | |
public function register(Application $app) | |
{ | |
$app['resolver'] = $app->share( | |
function ($app) { | |
return new ControllerResolver($app, $app['logger']); | |
} | |
); | |
} | |
public function boot(Application $app) | |
{ | |
} | |
} | |
// in Myapp\Routing\ControllerResolver.php | |
namespace Myapp\Routing; | |
use Bolt\Routing\ControllerResolver as DefaultControllerResolver; | |
use Symfony\Component\HttpFoundation\Request; | |
class ControllerResolver extends DefaultControllerResolver | |
{ | |
public function getController(Request $request) | |
{ | |
$controller = $request->attributes->get('_controller'); | |
if (isset($app['controller.'.$controller])) { | |
return $app['controller.'.$controller]; | |
} | |
return parent::getController($request); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment