在嵌入控制器中获取请求参数


Get request parameter in embedding controller

在Symfony1中,我可以:

blog:
  url:   /blog/slug
  param: { module: blog, action: index }

在操作/控制器中,我可以通过以下方式获得 slug:$request->getParameter('slug');

在 Symfony2 中:

blog:
    path:      /blog/{slug}
    defaults:  { _controller: AcmeBlogBundle:Blog:show }

我创建了与Symfony1相同的"组件" - http://symfony.com/doc/current/book/templating.html#templating-embedding-controller

如何在嵌入控制器中获取 slug?我试过了:

$request->query->get('foo');
$request->request->get('bar');

但这仍然返回 null。在 AcmeBlogBundle:Blog:show 控制器工作正常。

param 转换器将使用路由中的字符串填充参数。 所以这是你的方法的样子。

class BlogController extends Controller {
    public function showAction($slug) {
        // $slug will contain the value of the string from the route
    }
}

因此,如果您想将其嵌入到树枝模板中,它将如下所示:

{{ render( controller('AcmeBlogBundle:Blog:show', {'slug': 'this-is-the-slug' })) }}

或来自其他控制器

$this->render('AcmeBlogBundle:Blog:show.html.twig', array('slug' => 'this-is-the-slug'));