dragon202076 2012-05-09 12:46
浏览 39
已采纳

如何在Symfony 2中嵌入子命名空间控制器的动作?

I'm trying to render an action which is in a sub-namespaced controller from within a Twig view in Symfony 2.

The problem is: The render helper cannot find the controller/action because it's in a namespace below Controller.

This is my controller: src/Acme/Bundle/TestBundle/Controller/FirstModule/ExampleController.php

namespace Acme\Bundle\TestBundle\Controller\FirstModule;

class ExampleController extends Controller
{
    public function exampleAction(Request $request)
    {
        return $this->render('AcmeTestBundle:FirstModuleExample:example.html.twig');
    }

    public function embedAction(Request $request)
    {
        return $this->render('AcmeTestBundle:FirstModuleExample:embed.html.twig');
    }
}

This is my view: src/Acme/Bundle/TestBundle/Resources/views/FirstModuleExample/example.html.twig

{% render "AcmeTestBundle:Example:embed" %}
// or
{% render "Acme\Bundle\TestBundle\Controller\FirstModule\Example:example" %}
// or
{% render "Acme\Bundle\TestBundle\Controller\FirstModule\ExampleController:exampleAction" %}

I have read the Embedding Controllers documentation but no clue there how to handle Controllers that are in a sub-namespace.

Thanks.

  • 写回答

3条回答 默认 最新

  • dousi8237 2012-05-09 13:32
    关注

    Either of these should work. Remember, backslashes in strings need to be escaped (i.e., doubled)

    {% render "AcmeTestBundle:FirstModule\\Example:embed" %}
    

    or

    {% render "Acme\\Bundle\\TestBundle\\Controller\\FirstModule\\ExampleController::embedAction" %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部