dragon202076 2012-05-09 20: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 21: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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度