Silex Dynamic Routes - simple implementation

Posted: 15 января 2014


$app->get('/{controller}/{action}', function(Silex\Application $app, Request $request, $controller, $action){
    $controllerName = 'controller.' . $controller;

    // if controller requested
    if (!empty($app[$controllerName]) && $app[$controllerName] instanceof Controller) {

        // if controller action is available
        if(is_callable(array($app[$controllerName], $action))) {
            return $app[$controllerName]->$action($request);
        }
    }

    $app->abort(
        404,
        sprintf(
            'No route found for: %s:%s',
            $controller,
            $action
        )
    );
})
->assert('controller', '[A-z]+')
->assert('action', '[A-z]+');

If you find the post helpful you can share it


Ваше мнение:

Имя
   other with Symfony2
drcreazy © 2008-2012 all rights reserved