phpsymfonysilex

How to get a parameter from a url in Silex


I'm trying to get a parameter from an url in a Silex app.

Here's my connect method of my controller :

public function connect(Application $app)
{
    $controllers = $app['controllers_factory'];

    $controllers->get('/list/pois/{id}', array($this, 'actionPoisList'))
        ->before(array($this, 'controlerAuthentification'));

    return $controllers;
}

Here I'm trying to catch this parameter by doing this:

/**
 * @param Application $app
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 */
public function actionPoisList(Application $app){

    return $app->json($app->escape($app['id']));
}

Obviously, its not working so any alternative please. Thanks


Solution

  • The parameters in URLs are automagically passed into your controller routes if you specify them in the parameter's list:

    /**
     * @param Application $app
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function actionPoisList(Application $app, $id){
    
        return $app->json($app->escape($id));
    }
    

    Take into account that the route parameter and the function parameter should be named exactly the same.