0

Symfony1 では次のことができます:

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

アクション/コントローラーでは、次のようにスラッグを取得できます: $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

埋め込みコントローラーでスラッグを取得するにはどうすればよいですか? 私は試した:

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

しかし、これはまだnullを返します。AcmeBlogBu​​ndle:Blog:show コントローラーで問題なく動作しています。

4

1 に答える 1

2

param コンバーターは、引数にルートからの文字列を設定します。したがって、ここにあなたの方法がどのように見えるかがあります。

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

したがって、これを twig テンプレートに埋め込みたい場合は、次のようになります。

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

または別のコントローラーから

$this->render('AcmeBlogBundle:Blog:show.html.twig', array('slug' => 'this-is-the-slug'));
于 2013-09-26T19:33:32.363 に答える