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->se开发者_如何转开发tLayoutPath(('/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
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.
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.
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.
精彩评论