[英]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. 与C#.NET MVC 4.0类似。
In my view I may have: 我认为我可能有:
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. 我极有可能在页面的顶部始终保留Header,Normal nav和Footer部分。
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 模板视图: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 导航控制器:Controller / NavigationController
class NavigationController extends Controller
{
public function indexAction()
{
return $this->render(SomethingBundle:Navigation:index.html.php');
}
}
Navigation View: index.html.php 导航视图: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'
)
) ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.