(note (code cslai))

Hello World from Zend MVC

This post continued from this post. Finally I have found some time to start developing my pet project using Zend Framework. After getting the controller to work the way I am more familiar (comparing to Kohana which I used at work) with, the next step is to get it to output some data.

Base Action Controller

When I used other frameworks like Kohana or CodeIgniter, without extra modules installed, I would have to define view scripts specifically. However, in Zend_Controller, a view is likely to be defined properly and is waiting for data to be populated in. However, I still don’t get any output after a few attempts so I went to compare my dispatch method in the derived action controller with Zend_Controller_Action. The following code shows the updated dispatch method:


public function dispatch($actionName) {
$this->_helper->notifyPreDispatch();
$this->preDispatch();
$parameters = array();
foreach($this->_parametersMeta($actionName) as $paramMeta) {
$parameters = array_merge(
$parameters,
$this->_parameter($paramMeta, $this->_getAllParams())
);
}
call_user_func_array(array(&$this, $actionName), $parameters);
$this->postDispatch();
$this->_helper->notifyPostDispatch();
}

Apparently there are some ‘hooks’ that needs to be called throughout the dispatching process. Besides that, I want my view scripts to be loaded from another folder instead of the default ones, so I added a init method in the base action controller, as follows:


public function init() {
$this->view->addScriptPath(sprintf('%s/View', APP_PATH));
}

The script path should be defined during bootstrap phase, and I will post up a better solution once I find the exact place to configure it. Now that we have our base action controller fixed, let’s move to the actual action controller.

Action Controller

I am not sure whether action stack is HMVC, however, I like the way it works (although it is evil). I will probably stick to action stack for the time being while finding out how partial view can help in widgetizing my page. Besides having a view defined automatically, to put them into a layout I made a call in my bootstrap script, as follows:


Zend_Layout::startMvc(array(
'layoutPath' => APP_PATH . '/View/_layout'
));

Then I did the action stack thingy as follows:


class Controller_Index
extends Coolsilon_Controller_Base {
public function index() {
$this->_helper->actionStack('menu', 'index');
}
public function menu() {
$this->_helper->viewRenderer->setResponseSegment('menu');
}
}

Then, in my View/_layout/layout.phtml,


< ?php echo $this->layout()->menu; ?>
< ?php echo $this->layout()->content; ?>

View/index/index.phtml

This is the main content.

View/index/menu.phtml

This is the menu.

Then by visiting the project site, then I get the following output


This is the menu.
This is the main content.

Phew, I can’t believe I spent so much time on this easy stuff, XD. Now I am one step closer in writting the actual website, now what kind of website should I make?

Exit mobile version