The various MVC components -- request, router, dispatcher, action controller, and response objects -- may each throw exceptions on occasion. Some exceptions may be conditionally overridden, and others are used to indicate the developer may need to consider their application structure.
As some examples:
-
Zend_Controller_Dispatcher::dispatch()will, by default, throw an exception if an invalid controller is requested. There are two recommended ways to deal with this.-
Set the useDefaultControllerAlways parameter.
In your front controller, or your dispatcher, add the following directive:
<?php
$front->setParam('useDefaultControllerAlways', true);
// or
$dispatcher->setParam('useDefaultControllerAlways', true);When this flag is set, the dispatcher will use the default controller and action instead of throwing an exception. The disadvantage to this method is that any typos a user makes when accessing your site will still resolve and display your home page, which can wreak havoc with search engine optimization.
The exception thrown by
dispatch()is aZend_Controller_Dispatcher_Exceptioncontaining the text 'Invalid controller specified'. Use one of the methods outlined in the previous section to catch the exception, and then redirect to a generic error page or the home page.
-
-
Zend_Controller_Action::__call()will throw aZend_Controller_Action_Exceptionif it cannot dispatch a non-existent action to a method. Most likely, you will want to use some default action in the controller in cases like this. Ways to achieve this include:-
Subclass
Zend_Controller_Actionand override the__call()method. As an example:<?php
class My_Controller_Action extends Zend_Controller_Action
{
public function __call($method, $args)
{
if ('Action' == substr($method, -6)) {
$controller = $this->getRequest()->getControllerName();
$url = '/' . $controller . '/index';
return $this->_redirect($url);
}
throw new Exception('Invalid method');
}
}The example above intercepts any undefined action method called and redirects it to the default action in the controller.
-
Subclass
Zend_Controller_Dispatcherand override thegetAction()method to verify the action exists. As an example:<?php
class My_Controller_Dispatcher extends Zend_Controller_Dispatcher
{
public function getAction($request)
{
$action = $request->getActionName();
if (empty($action)) {
$action = $this->getDefaultAction();
$request->setActionName($action);
$action = $this->formatActionName($action);
} else {
$controller = $this->getController();
$action = $this->formatActionName($action);
if (!method_exists($controller, $action)) {
$action = $this->getDefaultAction();
$request->setActionName($action);
$action = $this->formatActionName($action);
}
}
return $action;
}
}The above code checks to see that the requested action exists in the controller class; if not, it resets the action to the default action.
This method is nice because you can transparently alter the action prior to final dispatch. However, it also means that typos in the URL may still dispatch correctly, which is not great for search engine optimization.
-
Use
Zend_Controller_Action::preDispatch()orZend_Controller_Plugin_Abstract::preDispatch()to identify invalid actions.By subclassing
Zend_Controller_Actionand modifyingpreDispatch(), you can modify all of your controllers to forward to another action or redirect prior to actually dispatching the action. The code for this will look similar to the code for overriding__call(), above.Alternatively, you can check this information in a global plugin. This has the advantage of being action controller independent; if your application consists of a variety of action controllers, and not all of them inherit from the same class, this method can add consistency in handling your various classes.
As an example:
<?php
class My_Controller_PreDispatchPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$front = Zend_Controller_Front::getInstance();
$dispatcher = $front->getDispatcher();
$class = $dispatcher->getControllerClass($request);
if (!$class) {
$class = $dispatcher->getDefaultControllerClass($request);
}
$r = new ReflectionClass($class);
$action = $dispatcher->getActionMethod($request);
if (!$r->hasMethod($action)) {
$defaultAction = $dispatcher->getDefaultAction();
$controllerName = $request->getControllerName();
$response = $front->getResponse();
$response->setRedirect('/' . $controllerName
. '/' . $defaultAction);
$response->sendHeaders();
exit;
}
}
}In this example, we check to see if the action requested is available in the controller. If not, we redirect to the default action in the controller, and exit script execution immediately.
-




