简体   繁体   中英

Symfony2 - call a controller from within a view

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:   -+ 
<?php echo $view['actions']->render(
    new \Symfony\Component\HttpKernel\Controller\ControllerReference(
        'YourBundle:NavigationController:index'
    )
) ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM