douqixia7942 2013-10-09 11:16
浏览 45
已采纳

Symfony2 - 从视图中调用控制器

I want to have a view call upon controllers from within it. Similar to C#.NET MVC 4.0.

In my view I may have:

  • Header
  • Admin navigation
  • Normal navigation
  • Page content ( divided into three parts, list of something, list of something else, main page content
  • Footer

All of these parts are reusable sections. I'm most likely going to always have a Header, Normal nav and Footer part of my page.

My controller does database calls to fetch the model and pump it to my view. I don't want to ever have to worry about this code again so I just want to call the result of this controller from any view: pseudo code:

Template view: base.html.php

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php $view['slots']->output('title', 'Welcome!') ?></title>
        <?php $view['slots']->output('stylesheets') ?>
        <link rel="shortcut icon" href="<?php echo $view['assets']->getUrl('favicon.ico') ?>" />
    </head>
    <body>
        <?php echo $view['actions']->render(SomethingBundle:Navigation:index); ?> // THIS LINE HERE
        <h1><?php $view['slots']->output('pageTitle') ?></h1>
        <?php $view['slots']->output('_content') ?>
    </body>

So this is my template page, and I expect to be able to include the navigation view in this page, but the navigation requires a model so it also depends on the navigation controller.

Navigation controller: Controller/NavigationController

class NavigationController extends Controller
    {
        public function indexAction()
        {
            return $this->render(SomethingBundle:Navigation:index.html.php');
        }
    }

Navigation View: index.html.php

<ul>
    <li>Home</li>
    <li>About</li>
</ul>

Obviously the above code does not work. How do you include the result of a controller in a view?

the error I get from the above code is:

[2/2] NotFoundHttpException: No route found for "GET Navigation:index"  -+  
[1/2] ResourceNotFoundException:   -+ 
  • 写回答

1条回答 默认 最新

  • duanduan1993 2013-10-09 11:39
    关注
    <?php echo $view['actions']->render(
        new \Symfony\Component\HttpKernel\Controller\ControllerReference(
            'YourBundle:NavigationController:index'
        )
    ) ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥50 成都蓉城足球俱乐部小程序抢票