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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测