views:

100

answers:

3

Hello guys,

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

A: 

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.

Mike
thanks, fixed that. But i think my main problem remains that i don't know how to inject the view correctly into the controller .. hm
Georg Wächter
Based on ZF 1.8.0: You said the view is needed during instantiation - that is not correct. The constructor only sets the request/response/args/helper and calls the init() method (note: you don't need to call init() yourself). The view is automatically created by initView() unless you have already defined it, which you have done. As far as I can see, you are defining the view correctly. Is the view still not working, or are you just concerned about whether or not you're doing it correctly?
Mike
+1  A: 

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.

blockhead
works great, thanks!
Georg Wächter
I would like to add to this, you might want to get a handle on the $response object as well, instead of just blindly outputting it.
blockhead
+1  A: 

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.

Ivan Krechetov