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>
        <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') ?>" />
        <?php echo $view['actions']->render(SomethingBundle:Navigation:index); ?> // THIS LINE HERE
        <h1><?php $view['slots']->output('pageTitle') ?></h1>
        <?php $view['slots']->output('_content') ?>

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


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
    duanduan1993 2013-10-09 11:39
    <?php echo $view['actions']->render(
        new \Symfony\Component\HttpKernel\Controller\ControllerReference(
    ) ?>
    点赞 评论