Zend Framework 中可以手动调度吗?

发布于 2024-08-12 18:12:50 字数 874 浏览 12 评论 0原文

我想知道如何手动开始执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-08-19 18:12:50

如果您使用 Zend_Application,那么您需要做的就是这样:

$application->bootstrap();
$request = new Zend_Controller_Request_Http();
// Set some parameters for request possibly
$controller = $controller = new IndexController($request, new Zend_Controller_Response_Http());

$controller->dispatch('hinweisAction');

Zend_Application 将负责为您设置视图。调用dispatch将会处理Action Helpers,特别是ViewRenderer,它会为你做所有脏活。

If you're using Zend_Application, all you need to do is something like this:

$application->bootstrap();
$request = new Zend_Controller_Request_Http();
// Set some parameters for request possibly
$controller = $controller = new IndexController($request, new Zend_Controller_Response_Http());

$controller->dispatch('hinweisAction');

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.

寒冷纷飞旳雪 2024-08-19 18:12:50

我建议您查看 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 the bootstrap() and dispatch() functions. May be you can just copy that verbatim.

烟火散人牵绊 2024-08-19 18:12:50

尝试:

$布局->内容=
$controller->view->render();

$view 指的是 $view 的本地实例。一旦将其分配给 $controller->view,$controller->init() 和 $controller->hinweisAction() 对它所做的任何工作都会影响 $controller->view,而不是本地 $view 对象。

Try:

$layout->content =
$controller->view->render();

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文