Zend Framework migration from 0.2.0 to 0.6.0


If you want to have your ZF aplication still working after migration from 0.2.0 to 0.6.0, you should make some changes in your code. However, it's desribed in manual, but only for Zend_Controller classes. There are some tips:

  1. Another way of setting the rewrite base
  2. Add second parameter to Zend_Filter_Input contructor call
  3. Change constructors __construct() in action controllers to init() methods
  4. Extend Zend_View class
  5. Turn on throwing exceptions by front controller
  6. Add echo before $this->render() methods calls in view files
  7. Delete base dir from first parameter of _redirect() methods in action controllers
  8. Add notfound plugin to emulate noRoute method
  9. API change in Zend_Http_Client class

1. Another way of setting the rewrite base

Change the way of setting rewrite base. Now we can do it in front controller, instead of setting this up in router. This is old way:

PHP:
  1. <?php
  2. $router->setRewriteBase($appPath);
  3. ?>

and now this should be:

PHP:
  1. <?php
  2. $frontcontroller->setBaseUrl($appPath);
  3. ?>

2. Add second parameter to Zend_Filter_Input contructor call

If you create instances od Zend_Filter_Input objects, make sure that the array isn't cleaned after that. You can achieve this by adding second parameter to constructors call. Instead of:

PHP:
  1. <?php
  2. $get = new Zend_Filter_Input($_GET);
  3. ?>

there should be:

PHP:
  1. <?php
  2. $get = new Zend_Filter_Input($_GET, false);
  3. ?>

3. Change constructors __construct() in action controllers to init() methods

If you are using constructors in action coontrollers you should change them to init() methods. Intead of:

PHP:
  1. <?php
  2. class ArticleController extends Zend_Controller_Action
  3. {
  4.     public function __construct()
  5.     {
  6.         // [...]
  7.     }
  8.     // [...]
  9. }
  10. ?>

there should be:

PHP:
  1. <?php
  2. class ArticleController extends Zend_Controller_Action
  3. {
  4.     public function init()
  5.     {
  6.         // [...]
  7.     }
  8.     // [...]
  9. }
  10. ?>

4. Extend Zend_View class

If you want to avoid notices informing about undefined variables you should create own view class and override offsetGet() method of ArrayObject. Our extended class should look like this:

PHP:
  1. <?php
  2.  
  3. class Foo_View extends Zend_View_Abstract
  4. {
  5.  
  6.     protected function _run()
  7.     {
  8.         include func_get_arg(0);
  9.     }
  10.    
  11.     public function offsetGet($index)
  12.     {
  13.         if (! $this->offsetExists($index)) {
  14.             return null;
  15.         }
  16.         return parent::offsetGet($index);
  17.     }
  18.  
  19. }
  20. ?>

Naturally, you should create instance of your new class instead of creating Zend_View instance.

5. Turn on throwing exceptions by front controller

If you want to throw exceptions in front controller you should set in in front controller, before dispatching:

PHP:
  1. <?php
  2. $controller->throwExceptions(true);
  3. ?>

6. Add echo before $this->render() methods calls in view files

In view files you should echo returns of render methods. So instead of:

PHP:
  1. <?php
  2. $this->render('foo.tpl');
  3. ?>

there should be:

PHP:
  1. <?php
  2. echo $this->render('foo.tpl');
  3. ?>

7. Delete base dir from first parameter of _redirect() methods in action controllers

If you are using _redirect() method with redirection adress containing rewrite base, you should delete rewrite base because now it's added automatically. If you were creating redirection like this:

PHP:
  1. <?php
  2. $this->_redirect('/admin/article/index/');
  3. ?>

Now you should:

PHP:
  1. <?php
  2. $this->_redirect('article/index/');
  3. ?>

Rewrite base (in this case "/admin/") will be prepended to redirection adress.

8. Add notfound plugin to emulate noRoute method

If you want to have noRoute method still working in 0.6.0 you should register plugin in front controller:

PHP:
  1. <?php
  2.  
  3. class Foo_Controller_Plugin_Notfound extends Zend_Controller_Plugin_Abstract
  4. {
  5.  
  6.     public function preDispatch($request)
  7.     {
  8.         $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
  9.  
  10.         if (!$dispatcher->isDispatchable($request))
  11.         {
  12.             $request->setControllerName($dispatcher->getDefaultController())
  13.             ->setActionName('noroute')
  14.             ->setDispatched(false);
  15.         }
  16.     }
  17.  
  18. }
  19. ?>

9. API change in Zend_Http_Client class

Now you should pass url to Zend_Http_Client constructor:

PHP:
  1. <?php
  2. $http = new Zend_Http_Client('http://example.org/');
  3. ?>

Next add http headers:

PHP:
  1. <?php
  2. $headers = array(
  3. 'Accept' => 'text/html',
  4. 'Referer' => 'http://cms.naruniec.info',
  5. 'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
  6. );
  7. $http->setHeaders($headers);
  8. ?>

and finally get the GET response:

PHP:
  1. <?php
  2. $httpResponse = $http->request();
  3. ?>

Before 0.6.0 we could pass headers to constructor, and we could get a GET response calling $http->get() method.

Theese tips should help in migration in most essential cases. More informations you can find in manual in chapter about Migrating from 0.2.0 or before to 0.6.0

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Zend Framework 1.5 has been released!
Zend Framework Widget
Polecane
Zend Studio - The premiere PHP IDE

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!