on October 20, 2008. in Development, Programming, Software. A 4 minute read.
This post is the second part of my introductory text on Zend Framework, Starting with Zend Framework. This time I cover the basics about controllers, actions, view scripts and view helpers. On request routing and the Front Controller I will write one (or more) big post(s), so this part won’t be explained now. I will also skip explaining the models; they deserve their own post :)
If anyone is into writing a guest-post on models, let me know!
The Controllers are the heart of every MVC based application. They control the execution of the application, what to do with the data, what to show the user, what to write to the database, etc. The Controllers that you will write all the time, are called Action Controllers. These Controllers subclass the Zend_Controller_Action abstract class. Every application module must have a default Controller, which will be accessed if no specific Controller is requested. The default name for this default Controller is Index. Examples of the IndexController and FooController:
<?php
// The IndexController class must be placed in the controllers folder
// and saved as IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
}
}
// The FooController class must be placed in the controllers folder
// and saved as FooController.php
class FooController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
}
public function barAction()
{
}
public function someRandomFunctionDoingSomeFunkyStuff()
{
}
}
The Controllers must contain at least the indexAction()
function; the others are arbitrary. I always have an init()
function, in which I setup the cache object, call up the models, etc. Controller names that are not in the default module, must be prefixed with the Title-cased name of the module and an underscore:
<?php
// An example of the IndexController in the
// dummy module
// The file name remains IndexController.php!!!
class Dummy_IndexController extends Zend_Controller_Action
{
}
// An example of the FooController in the
// dummy module
// The file name remains FooController.php!!!
class Dummy_FooController extends Zend_Controller_Action
{
}
Actions are methods of the Controller class. Use them to do some specific task: show users, list news, insert to database (the actual INSERT SQL statement should be in the model), etc. As stated before, every Controller must have an index action this one is called if no specific action is requested. By default the view object is instantiated, so if you don’t turn it off, you must create a view script with the same name as the action (without the Action word) in the views/scripts/foo/
folder.
Assigning variables to the view scripts is simple:
public function indexAction()
{
$this->view->someVariable = "some value...";
}
View scripts are, well, for viewing. This is the only place where you should have statements like echo
and print
. The default templating engine is PHP itself, but it’s possible to change it to something like Smarty. I leave PHP; it has everything for templating, so why would I change it? The default file extension for view scripts is phtml but as with everything, this can also be changed :)
Getting variables that are assigned from the action:
// Output: some value...
<?= this->someVariable ?>
The view helpers are simple classes that help in view scripts with things like formatting dates, creating links, etc. Here’s an example view helper that I use to show dates in Serbian format:
File name: views/helpers/SrDateFormat.php
<?php
/**
* View helper for returning dates in Serbian format
* dd.mm.yyyy.
*
*/
class Zend_View_Helper_SrDateFormat
{
public function srDateFormat($dateToFormat)
{
return date('d.m.Y.', strtotime($dateToFormat));
}
}
Usage is quite simple:
// somewhere in some view script...
<?= $this->srDateFormat($someDateToShow); ?>
Just for an overview, here is an example of a Foo Controller in the Dummy module with index and bar actions and their view scripts.
<?php
// File name: application/dummy/controllers/FooController.php
class Dummy_FooController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->sayHello = "Hi there!";
}
public function barAction()
{
$this->view->sayHelloAgain = "Hi here :)";
}
}
And the view scripts:
<!-- File name: application/dummy/views/scripts/foo/index.phtml -->
<h1>Saying hello</h1>
<?= $this->sayHello ?>
<!-- File name: application/dummy/views/scripts/foo/bar.phtml -->
<h1>Saying hello again</h1>
<?= $this->sayHelloAgain ?>
So if you direct your browser to http://example.com/dummy/foo/ or to http://example.com/dummy/foo/bar you should get the Saying hello or the Saying hello again page…
This would be my introductory text to Zend Framework. Hope it’s not confusing and is easy to follow. I just want to help newcomers to ZF help settling in easily :) For a tutorial application with ZF, I recommend Rob Allen’s Zend Framework tutorial.
In the coming days/weeks I’ll write a detailed post about the Front Controller, so if you wish, grab the feed or subscribe by E-mail to stay tuned.
Cheers!