如何从 Silex 中的 url 获取参数

Kub*_*hon 5 php symfony silex

我正在尝试从 Silex 应用程序中的 url 获取参数。

这是我的控制器的连接方法:

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

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

    return $controllers;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图通过这样做来捕获这个参数:

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

    return $app->json($app->escape($app['id']));
}
Run Code Online (Sandbox Code Playgroud)

显然,它不起作用,所以请有其他选择。谢谢

has*_*dic 1

如果您在参数列表中指定了 URL 中的参数,它们会自动传递到您的控制器路由中:

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

    return $app->json($app->escape($id));
}
Run Code Online (Sandbox Code Playgroud)

请注意,路由参数和函数参数的名称应完全相同。