Zend Framework 中可以手动调度吗?
我想知道如何手动开始执行 MVC 应用程序的控制器操作。我的目标是使用简单的 include 将 /myController/myAction 的 html 输出集成到另一个 php 应用程序 (typo3) 中。我想到手动实例化控制器、视图和布局,以绕过调度程序。可悲的是我无法让它工作。我目前的做法是这样的:
// standard bootstrap ... setting up autoloader, database etc.
$layout = new Zend_Layout();
$layout->setLayoutPath(('/application/default/layouts'));
$layout->setLayout('main');
$layout->setContentKey('content');
$view = new Zend_View();
$controller = new IndexController(new Zend_Controller_Request_Http($currenUrl), new Zend_Controller_Response_Http());
$controller->view = $view;
$controller->init();
$controller->hinweisAction();
$layout->content = $view->render();
echo $layout->render();
实例化布局没有问题,但是创建控制器时就变得复杂了。在调用构造函数后设置视图实例不起作用,因为在实例化期间已经需要视图。
对于这种情况,“正确”的方法是什么?也许实现一个简单的用户定义的调度程序,它使用我预定义的控制器和操作名称?
问候
格奥尔格·瓦希特
i'm wondering how i can manually start execute a controller action of my MVC application. My goal is to integrate the html output of /myController/myAction into another php application (typo3) using a simple include. I thought of manually instantiating the controller, the view and the layout, to bypass the dispatcher. Sadly i can't get it working. My current approach looks like this:
// standard bootstrap ... setting up autoloader, database etc.
$layout = new Zend_Layout();
$layout->setLayoutPath(('/application/default/layouts'));
$layout->setLayout('main');
$layout->setContentKey('content');
$view = new Zend_View();
$controller = new IndexController(new Zend_Controller_Request_Http($currenUrl), new Zend_Controller_Response_Http());
$controller->view = $view;
$controller->init();
$controller->hinweisAction();
$layout->content = $view->render();
echo $layout->render();
Instantiating the layout is no problem, but it becomes complicated when creating the controller. Setting the view instance after calling the constructor does not work, because the view is needed already during the instantiation.
What would be the "right" way for such a scenario? Maybe implement a simple user defined dispatcher which uses predefined controller and action names from me?
Greets
Georg Wächter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Zend_Application,那么您需要做的就是这样:
Zend_Application 将负责为您设置视图。调用dispatch将会处理Action Helpers,特别是ViewRenderer,它会为你做所有脏活。
If you're using Zend_Application, all you need to do is something like this:
Zend_Application will take care of setting up your view for you. Calling dispatch will take care of Action Helpers, particularly the ViewRenderer which does all the dirty work for you.
我建议您查看
Zend_Test_PHPUnit_ControllerTestCase
源代码。它完全可以满足您对控制器生成的内容运行测试所需的功能。具体来说,请阅读bootstrap()
和dispatch()
函数。也许您可以逐字复制该内容。I'd suggest you looking at
Zend_Test_PHPUnit_ControllerTestCase
source code. It does exactly what you need to run tests against the content generated by a controller. Specifically, read thebootstrap()
anddispatch()
functions. May be you can just copy that verbatim.尝试:
$view 指的是 $view 的本地实例。一旦将其分配给 $controller->view,$controller->init() 和 $controller->hinweisAction() 对它所做的任何工作都会影响 $controller->view,而不是本地 $view 对象。
Try:
$view is referring to the local instance of $view. Once you assign it to $controller->view, any work that $controller->init() and $controller->hinweisAction() does on it will affect $controller->view, not the local $view object.