7.9. Migrating from Previous Versions

The API of the MVC components has changed over time. If you started using Zend Framework in an early version, follow the guidelines below to migrate your scripts to use the new architecture.

7.9.1. Migrating from 0.6.0 to 0.8.0

Per previous changes, the most basic usage of the MVC components remains the same:

require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('/path/to/controllers');

However, the directory structure underwent an overhaul, several components were removed, and several others either renamed or added. Changes include:

  • Zend_Controller_Router was removed in favor of the rewrite router.

  • Zend_Controller_RewriteRouter was renamed to Zend_Controller_Router_Rewrite, and promoted to the standard router shipped with the framework; Zend_Controller_Front will use it by default if no other router is supplied.

  • A new route class for use with the rewrite router was introduced, Zend_Controller_Router_Route_Module; it covers the default route used by the MVC, and has support for controller modules.

  • Zend_Controller_Router_StaticRoute was renamed to Zend_Controller_Router_Route_Static.

  • Zend_Controller_Dispatcher was renamed Zend_Controller_Dispatcher_Standard.

  • Zend_Controller_Action::_forward()'s arguments have changed. The signature is now:

    final protected function _forward($action, $controller = null, $module = null, array $params = null);
    

    $action is always required; if no controller is specified, an action in the current controller is assumed. $module is always ignored unless $controller is specified. Finally, any $params provided will be appended to the request object. If you do not require the controller or module, but still need to pass parameters, simply specify null for those values.

7.9.2. Migrating from 0.2.0 or before to 0.6.0

The most basic usage of the MVC components has not changed; you can still do each of the following:

require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('/path/to/controllers');
/* -- create a router -- */
$router = new Zend_Controller_RewriteRouter();
$router->addRoute('user', 'user/:username', array('controller' => 'user',
'action' => 'info'));

/* -- set it in a controller -- */
$ctrl = Zend_Controller_Front::getInstance();
$ctrl->setRouter($router);

/* -- set controller directory and dispatch -- */
$ctrl->setControllerDirectory('/path/to/controllers');
$ctrl->dispatch();

We encourage use of the Response object to aggregate content and headers. This will allow for more flexible output format switching (for instance, JSON or XML instead of XHTML) in your applications. By default, dispatch() will render the response, sending both headers and rendering any content. You may also have the front controller return the response using returnResponse(), and then render the response using your own logic. A future version of the front controller may enforce use of the response object via output buffering.

There are many additional features that extend the existing API, and these are noted in the documentation.

The main changes you will need to be aware of will be found when subclassing the various components. Key amongst these are:

  • Zend_Controller_Front::dispatch() by default traps exceptions in the response object, and does not render them, in order to prevent sensitive system information from being rendered. You can override this in several ways:

    • Set throwExceptions() in the front controller:

      $front->throwExceptions(true);
      
    • Set renderExceptions() in the response object:

      $response->renderExceptions(true);
      $front->setResponse($response);
      $front->dispatch();
      
      // or:
      $front->returnResponse(true);
      $response = $front->dispatch();
      $response->renderExceptions(true);
      echo $response;
      
  • Zend_Controller_Dispatcher_Interface::dispatch() now accepts and returns a Paragraaf 7.1.2, “Request Object” object instead of a dispatcher token.

  • Zend_Controller_Router_Interface::route() now accepts and returns a Paragraaf 7.1.2, “Request Object” object instead of a dispatcher token.

  • Zend_Controller_Action changes include:

    • The constructor now accepts exactly three arguments, Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, and array $params (optional). Zend_Controller_Action::__construct() uses these to set the request, response, and invokeArgs properties of the object, and if overriding the constructor, you should do so as well. Better yet, use the init() method to do any instance configuration, as this method is called as the final action of the constructor.

    • run() is no longer defined as final, but is also no longer used by the front controller; it's sole purpose is for using the class as a page controller. It now takes two optional arguments, a Zend_Controller_Request_Abstract $request and a Zend_Controller_Response_Abstract $response.

    • indexAction() no longer needs to be defined, but is encouraged as the default action. This allows using the RewriteRouter and action controllers to specify different default action methods.

    • __call() should be overridden to handle any undefined actions automatically.

    • _redirect() now takes an optional second argument, the HTTP code to return with the redirect, and an optional third argument, $prependBase, that can indicate that the base URL registered with the request object should be prepended to the url specified.

    • The _action property is no longer set. This property was a Zend_Controller_Dispatcher_Token, which no longer exists in the current incarnation. The sole purpose of the token was to provide information about the requested controller, action, and URL parameters. This information is now available in the request object, and can be accessed as follows:

      // Retrieve the requested controller name
      // Access used to be via: $this->_action->getControllerName().
      // The example below uses getRequest(), though you may also directly access the
      // $_request property; using getRequest() is recommended as a parent class may
      // override access to the request object.
      $controller = $this->getRequest()->getControllerName();
      
      // Retrieve the requested action name
      // Access used to be via: $this->_action->getActionName().
      $action = $this->getRequest()->getActionName();
      
      // Retrieve the request parameters
      // This hasn't changed; the _getParams() and _getParam() methods simply proxy to
      // the request object now.
      $params = $this->_getParams();
      $foo = $this->_getParam('foo', 'default'); // request 'foo' parameter, using
                                                 // 'default' as default value if not found
      
      
    • noRouteAction() has been removed. The appropriate way to handle non-existent action methods should you wish to route them to a default action is using __call():

      public function __call($method, $args)
      {
          // If an unmatched 'Action' method was requested, pass on to the default
          // action method:
          if ('Action' == substr($method, -6)) {
              return $this->defaultAction();
          }
          
          throw new Zend_Controller_Exception('Invalid method called');
      }
      
  • Zend_Controller_RewriteRouter::setRewriteBase() has been removed. Use Zend_Controller_Front::setBaseUrl() instead (or Zend_Controller_Request_Http::setBaseUrl(), if using that request class).

  • Zend_Controller_Plugin_Interface was replaced by Zend_Controller_Plugin_Abstract. All methods now accept and return a Paragraaf 7.1.2, “Request Object” object instead of a dispatcher token.