Zend_Controller
Using the controller is pretty intuitive. In fact, I'm writing this tutorial without the luxury of documentation!
Note: Documentation is now available at
http://framework.zend.com/manual/zend.controller.html.
I begin with
Zend_Controller_Front
, a front controller. In order to begin understanding how it works, place the following code in your
index.php
file:
<?php
include 'Zend.php';
Zend::loadClass('Zend_Controller_Front');
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('/path/to/controllers');
$controller->dispatch();
?>
<?php
include 'Zend.php';
Zend::loadClass('Zend_Controller_Front');
$controller = Zend_Controller_Front::getInstance()
->setControllerDirectory('/path/to/controllers')
->dispatch();
?>
/foo/bar
, you get an error. That's good! It lets you know something is happening. The major complaint is that
IndexController.php
is not found. Before you create this file, it's helpful to understand how the framework expects you to organize things. The framework breaks a request down into parts, and in the case of a request for
/foo/bar
,
foo
is the controller, and
bar
is the action. The default value for each is
index
. When
foo
is the controller, the framework looks for a file called
FooController.php
in the
controllers
directory. Because this does not exist, the framework falls back to
IndexController.php
. Not finding either, it reports the error. To continue, create
IndexController.php
in the
controllers
directory (which you set with
setControllerDirectory()
):
<?php
Zend::loadClass('Zend_Controller_Action');
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'IndexController::indexAction()';
}
}
?>
IndexController
class handles requests for which the controller is
index
or for which the indicated controller does not exist, as just explained. The
indexAction()
method handles requests for which the action is
index
. Remember that
index
is the default value for both the controller and the action. If you try a request for
/
,
/index
, or
/index/index
, the
indexAction()
method is executed. (Trailing slashes do not alter this behavior.) A request for any other resource is going to result in an error. There is another useful method to add to
IndexController
before continuing. The
noRouteAction()
method is called whenever a request is made for a controller that doesn't exist. For example, a request for
/foo/bar
executes
noRouteAction()
if
FooController.php
does not exist. However, a request for
/index/foo
still results in an error, because
foo
is the action, not the controller. Add
noRouteAction()
to
IndexController.php
:
<?php
Zend::loadClass('Zend_Controller_Action');
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'IndexController::indexAction()';
}
public function noRouteAction()
{
$this->_redirect('/');
}
}
?>
$this->_redirect('/')
to illustrate a possible action to take in
noRouteAction()
. This causes requests for nonexistent controllers to be redirected to the root document (front page). Now create
FooController.php
:
<?php
Zend::loadClass('Zend_Controller_Action');
class FooController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'FooController::indexAction()';
}
public function barAction()
{
echo 'FooController::barAction()';
}
}
?>
/foo/bar
, you should see that
barAction()
is being executed, because
bar
is the action. Not only can you already support friendly URLs, but you can also do so in a very organized way with just a few lines of code. Cool! You can also create a
__call()
method to handle requests for undefined actions such as
/foo/baz
:
<?php
Zend::loadClass('Zend_Controller_Action');
class FooController extends Zend_Controller_Action
{
public function indexAction()
{
echo 'FooController::indexAction()';
}
public function barAction()
{
echo 'FooController::barAction()';
}
public function __call($action, $arguments)
{
echo 'FooController:__call()';
}
}
?>