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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵