Previous Next

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.

Migrating from 1.5.x to 1.6.0 or newer

Dispatcher Interface changes

Users brought to our attention the fact that Zend_Controller_Front and Zend_Controller_Router_Route_Module were each using methods of the dispatcher that were not in the dispatcher interface. We have now added the following three methods to ensure that custom dispatchers will continue to work with the shipped implementations:

  • getDefaultModule(): should return the name of the default module.

  • getDefaultControllerName(): should return the name of the default controller.

  • getDefaultAction(): should return the name of the default action.

Migrating from 1.0.x to 1.5.0 or newer

Though most basic functionality remains the same, and all documented functionality remains the same, there is one particular undocumented "feature" that has changed.

When writing URLs, the documented way to write camelCased action names is to use a word separator; these are '.' or '-' by default, but may be configured in the dispatcher. The dispatcher internally lowercases the action name, and uses these word separators to re-assemble the action method using camelCasing. However, because PHP functions are not case sensitive, you could still write URLs using camelCasing, and the dispatcher would resolve these to the same location. For example, 'camel-cased' would become 'camelCasedAction' by the dispatcher, whereas 'camelCased' would become 'camelcasedAction'; however, due to the case insensitivity of PHP, both will execute the same method.

This causes issues with the ViewRenderer when resolving view scripts. The canonical, documented way is that all word separators are converted to dashes, and the words lowercased. This creates a semantic tie between the actions and view scripts, and the normalization ensures that the scripts can be found. However, if the action 'camelCased' is called and actually resolves, the word separator is no longer present, and the ViewRenderer attempts to resolve to a different location -- 'camelcased.phtml' instead of 'camel-cased.phtml'.

Some developers relied on this "feature", which was never intended. Several changes in the 1.5.0 tree, however, made it so that the ViewRenderer no longer resolves these paths; the semantic tie is now enforced. First among these, the dispatcher now enforces case sensitivity in action names. What this means is that referring to your actions on the url using camelCasing will no longer resolve to the same method as using word separators (i.e., 'camel-casing'). This leads to the ViewRenderer now only honoring the word-separated actions when resolving view scripts.

If you find that you were relying on this "feature", you have several options:

  • Best option: rename your view scripts. Pros: forward compatibility. Cons: if you have many view scripts that relied on the former, unintended behavior, you will have a lot of renaming to do.

  • Second best option: The ViewRenderer now delegates view script resolution to Zend_Filter_Inflector; you can modify the rules of the inflector to no longer separate the words of an action with a dash:

    getInflector();
    $inflector->setFilterRule(':action', array(
        new Zend_Filter_PregReplace(
            '#[^a-z0-9' . preg_quote(DIRECTORY_SEPARATOR, '#') . ']+#i',
            ''
        ),
        'StringToLower'
    ));
    ?>

    The above code will modify the inflector to no longer separate the words with dash; you may also want to remove the 'StringToLower' filter if you do want the actual view script names camelCased as well.

    If renaming your view scripts would be too tedious or time consuming, this is your best option until you can find the time to do so.

  • Least desirable option: You can force the dispatcher to dispatch camelCased action names with a new front controller flag, 'useCaseSensitiveActions':

    setParam('useCaseSensitiveActions', true);
    ?>

    This will allow you to use camelCasing on the url and still have it resolve to the same action as when you use word separators. However, this will mean that the original issues will cascade on through; you will likely need to use the second option above in addition to this for things to work at all reliably.

    Note, also, that usage of this flag will raise a notice that this usage is deprecated.

Migrating from 0.9.3 to 1.0.0RC1 or newer

The principal changes introduced in 1.0.0RC1 are the introduction of and default enabling of the ErrorHandler plugin and the ViewRenderer action helper. Please read the documentation to each thoroughly to see how they work and what effect they may have on your applications.

The ErrorHandler plugin runs during postDispatch() checking for exceptions, and forwarding to a specified error handler controller. You should include such a controller in your application. You may disable it by setting the front controller parameter noErrorHandler:

setParam('noErrorHandler', true);

The ViewRenderer action helper automates view injection into action controllers as well as autorendering of view scripts based on the current action. The primary issue you may encounter is if you have actions that do not render view scripts and neither forward or redirect, as the ViewRenderer will attempt to render a view script based on the action name.

There are several strategies you can take to update your code. In the short term, you can globally disable the ViewRenderer in your front controller bootstrap prior to dispatching:

setParam('noViewRenderer', true);

However, this is not a good long term strategy, as it means most likely you'll be writing more code.

When you're ready to start using the ViewRenderer functionality, there are several things to look for in your controller code. First, look at your action methods (the methods ending in 'Action'), and determine what each is doing. If none of the following is happening, you'll need to make changes:

  • Calls to $this->render()

  • Calls to $this->_forward()

  • Calls to $this->_redirect()

  • Calls to the Redirector action helper

The easiest change is to disable auto-rendering for that method:

$this->_helper->viewRenderer->setNoRender();

If you find that none of your action methods are rendering, forwarding, or redirecting, you will likely want to put the above line in your preDispatch() or init() methods:

public function preDispatch()
{
    // disable view script autorendering
    $this->_helper->viewRenderer->setNoRender()
    // .. do other things...
}

If you are calling render(), and you're using the Conventional Modular directory structure, you'll want to change your code to make use of autorendering:

  • If you're rendering multiple view scripts in a single action, you don't need to change a thing.

  • If you're simply calling render() with no arguments, you can remove such lines.

  • If you're calling render() with arguments, and not doing any processing afterwards or rendering multiple view scripts, you can change these calls to read $this->_helper->viewRenderer().

If you're not using the conventional modular directory structure, there are a variety of methods for setting the view base path and script path specifications so that you can make use of the ViewRenderer. Please read the ViewRenderer documentation for information on these methods.

If you're using a view object from the registry, or customizing your view object, or using a different view implementation, you'll want to inject the ViewRenderer with this object. This can be done easily at any time.

  • Prior to dispatching a front controller instance:

    
    
                
  • Any time during the bootstrap process:

    setView($view);

There are many ways to modify the ViewRenderer, including setting a different view script to render, specifying replacements for all replaceable elements of a view script path (including the suffix), choosing a response named segment to utilize, and more. If you aren't using the conventional modular directory structure, you can even associate different path specifications with the ViewRenderer.

We encourage you to adapt your code to use the ErrorHandler and ViewRenderer as they are now core functionality.

Migrating from 0.9.2 to 0.9.3 or newer

0.9.3 introduces action helpers. As part of this change, the following methods have been removed as they are now encapsulated in the redirector action helper:

  • setRedirectCode(); use Zend_Controller_Action_Helper_Redirector::setCode().

  • setRedirectPrependBase(); use Zend_Controller_Action_Helper_Redirector::setPrependBase().

  • setRedirectExit(); use Zend_Controller_Action_Helper_Redirector::setExit().

Read the action helpers documentation for more information on how to retrieve and manipulate helper objects, and the redirector helper documentation for more information on setting redirect options (as well as alternate methods for redirecting).

Migrating from 0.6.0 to 0.8.0 or newer

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.

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 The Request Object object instead of a dispatcher token.

  • Zend_Controller_Router_Interface::route() now accepts and returns a The 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 The Request Object object instead of a dispatcher token.

Previous Next
Introduction to Zend Framework
Overview
Installation
Zend_Acl
Introduction
Refining Access Controls
Advanced Use
Zend_Auth
Introduction
Database Table Authentication
Digest Authentication
HTTP Authentication Adapter
LDAP Authentication
Open ID Authentication
Zend_Cache
Introduction
The theory of caching
Zend_Cache frontends
Zend_Cache backends
Zend_Captcha
Introduction
Captcha Operation
Captcha Adapters
Zend_Config
Introduction
Theory of Operation
Zend_Config_Ini
Zend_Config_Xml
Zend_Console_Getopt
Introduction to Getopt
Declaring Getopt Rules
Fetching Options and Arguments
Configuring Zend_Console_Getopt
Zend_Controller
Zend_Controller Quick Start
Zend_Controller Basics
The Front Controller
The Request Object
The Standard Router: Zend_Controller_Router_Rewrite
The Dispatcher
Action Controllers
Action Helpers
The Response Object
Plugins
Using a Conventional Modular Directory Structure
MVC Exceptions
Migrating from Previous Versions
Zend_Currency
Introduction to Zend_Currency
How to work with currencies
Migrating from Previous Versions
Zend_Date
Introduction
Theory of Operation
Basic Methods
Zend_Date API Overview
Creation of dates
Constants for General Date Functions
Working examples
Zend_Db
Zend_Db_Adapter
Zend_Db_Statement
Zend_Db_Profiler
Zend_Db_Select
Zend_Db_Table
Zend_Db_Table_Row
Zend_Db_Table_Rowset
Zend_Db_Table Relationships
Zend_Debug
Dumping Variables
Zend_Dojo
Introduction
Zend_Dojo_Data: dojo.data Envelopes
Dojo View Helpers
Dojo Form Elements and Decorators
Zend_Dom
Introduction
Zend_Dom_Query
Zend_Exception
Using Exceptions
Zend_Feed
Introduction
Importing Feeds
Retrieving Feeds from Web Pages
Consuming an RSS Feed
Consuming an Atom Feed
Consuming a Single Atom Entry
Modifying Feed and Entry structures
Custom Feed and Entry Classes
Zend_File
Zend_File_Transfer
Validators for Zend_File_Transfer
Zend_Filter
Introduction
Standard Filter Classes
Filter Chains
Writing Filters
Zend_Filter_Input
Zend_Filter_Inflector
Zend_Form
Zend_Form
Zend_Form Quick Start
Creating Form Elements Using Zend_Form_Element
Creating Forms Using Zend_Form
Creating Custom Form Markup Using Zend_Form_Decorator
Standard Form Elements Shipped With Zend Framework
Standard Form Decorators Shipped With Zend Framework
Internationalization of Zend_Form
Advanced Zend_Form Usage
Zend_Gdata
Introduction to Gdata
Authenticating with AuthSub
Authenticating with ClientLogin
Using Google Calendar
Using Google Documents List Data API
Using Google Spreadsheets
Using Google Apps Provisioning
Using Google Base
Using the YouTube Data API
Using Picasa Web Albums
Catching Gdata Exceptions
Zend_Http
Zend_Http_Client - Introduction
Zend_Http_Client - Advanced Usage
Zend_Http_Client - Connection Adapters
Zend_Http_Cookie and Zend_Http_CookieJar
Zend_Http_Response
Zend_InfoCard
Introduction
Zend_Json
Introduction
Basic Usage
JSON Objects
XML to JSON conversion
Zend_Json_Server - JSON-RPC server
Zend_Layout
Introduction
Zend_Layout Quick Start
Zend_Layout Configuration Options
Zend_Layout Advanced Usage
Zend_Ldap
Introduction
Zend_Loader
Loading Files and Classes Dynamically
Loading Plugins
Zend_Locale
Introduction
Using Zend_Locale
Normalization and Localization
Working with Dates and Times
Supported Languages for Locales
Supported Regions for Locales
Zend_Log
Overview
Writers
Formatters
Filters
Zend_Mail
Introduction
Sending via SMTP
Sending Multiple Mails per SMTP Connection
Using Different Transports
HTML E-Mail
Attachments
Adding Recipients
Controlling the MIME Boundary
Additional Headers
Character Sets
Encoding
SMTP Authentication
Securing SMTP Transport
Reading Mail Messages
Zend_Measure
Introduction
Creation of Measurements
Outputting measurements
Manipulating Measurements
Types of measurements
Zend_Memory
Overview
Memory Manager
Memory Objects
Zend_Mime
Zend_Mime
Zend_Mime_Message
Zend_Mime_Part
Zend_OpenId
Introduction
Zend_OpenId_Consumer Basics
Zend_OpenId_Provider
Zend_Paginator
Introduction
Usage
Configuration
Advanced usage
Zend_Pdf
Introduction.
Creating and loading PDF documents.
Save changes to the PDF document.
Document pages.
Drawing.
Document Info and Metadata.
Zend_Pdf module usage example.
Zend_Registry
Using the Registry
Zend_Rest
Introduction
Zend_Rest_Client
Zend_Rest_Server
Zend_Search_Lucene
Overview
Building Indexes
Searching an Index
Query Language
Query Construction API
Character Set
Extensibility
Interoperating with Java Lucene
Advanced
Best Practices
Zend_Server
Introduction
Zend_Server_Reflection
Zend_Service
Introduction
Zend_Service_Akismet
Zend_Service_Amazon
Zend_Service_Audioscrobbler
Zend_Service_Delicious
Zend_Service_Flickr
Zend_Service_Nirvanix
Zend_Service_ReCaptcha
Zend_Service_Simpy
Introduction
Zend_Service_StrikeIron
Zend_Service_StrikeIron: Bundled Services
Zend_Service_StrikeIron: Advanced Uses
Zend_Service_Technorati
Zend_Service_Yahoo
Zend_Session
Introduction
Basic Usage
Advanced Usage
Global Session Management
Zend_Session_SaveHandler_DbTable
Zend_Soap
Zend_Soap_Server
Zend_Soap_Client
WSDL Accessor
AutoDiscovery. Introduction
Class autodiscovering.
Functions autodiscovering.
Autodiscovering. Datatypes.
Zend_Test
Introduction
Zend_Test_PHPUnit
Zend_Text
Zend_Text_Figlet
Zend_TimeSync
Introduction
Working with Zend_TimeSync
Zend_Translate
Introduction
Adapters for Zend_Translate
Using Translation Adapters
Zend_Uri
Zend_Uri
Zend_Validate
Introduction
Standard Validation Classes
Validator Chains
Writing Validators
Zend_Version
Reading the Zend Framework Version
Zend_View
Introduction
Controller Scripts
View Scripts
View Helpers
Zend_View_Abstract
Zend_Wildfire
Zend_Wildfire
Zend_XmlRpc
Introduction
Zend_XmlRpc_Client
Zend_XmlRpc_Server
Zend Framework Requirements
PHP Version
PHP Extensions
Zend Framework Components
Zend Framework Dependencies
Zend Framework Coding Standard for PHP
Overview
PHP File Formatting
Naming Conventions
Coding Style
Copyright Information