Thursday, October 20, 2011

Preparing for CakePHP 2.0 : Views

CakePHP 2.0.0 has been released for a couple of days now.  I've been watching it go through the release candidate releases, but I haven't got my feet wet, or hands dirty until yesterday.

For anyone considering converting their CakePHP 1.X app to CakePHP 2.0, I recommend reading the migration guide, then reading the manual, from start to finishing, then read the migration guide again.

As a learning exercise, I'm converting one of my smaller projects to CakePHP 2.0.0.  I think I may have even skipped CakePHP 1.3, and jumped straight from CakePHP 1.2.

I've been following the migration guide, and as awesome as it is, there's so much stuff to think about.

So far, my approach has been as follows:

  • Delete the old cake directory, and global vendors and plugins, if they're empty.
  • Add in the new lib directory, containing the new Cake directory, and the global vendors and plugins directories.
  • Rename the app directories to match the new naming conventions.
  • Move app_controller.php to Controllers.
  • Update the Config files such as core.php, bootstrap.php and database.php.
  • Rename the controllers, components, models, behaviours, datasources, and helpers to the new naming convention.
  • Get a basic screen working! For me, that was the login screen, and even then, it was only the GET side of things that I wanted to get doing.
Make sure you're using some sort of source control, and check in after each step, or as often as you feel you need to do.  

If you're using Git and Windows, watch out for directory renames.  I ended up in a strange situation, where my app/config directory was split into app/config and app/Config in Git, even though there was only one app/Config directory on disk. I temporarily renamed to app/Configs, committed the change, then changed it back to app/Config.  That seemed to do the trick.

After getting the GET side of the login screen going, I've decided to convert all my Views first.  View conversion is fairly standard.  Even if you're not converting to CakePHP 2.0 right now (you're waiting for the first point release, right?) here are some things you can start doing now to make the conversion easier.

  • Stop using e(). It's gone in CakePHP 2.0, so just use echo.  There are a whole bunch of other functions that are going the way of the dodo, so perhaps hunt them down, and use PHP native alternatives now.
  • Start using echo __('Text', true); for your translatable strings, instead of relying on the echo inside __().  When you migrate, you can remove the ", true" at your leisure.  This is quicker than having to insert echo.
  • Stop using local variables for helpers.  You might have been in the habit of using $html->link().  You'll want to stop that now, and use $this->Html->link() instead, since the former won't work in CakePHP 2.0.
That's about all I've been doing with my views for the moment.  I've also been enjoying __() acting like sprintf(), and I've been able to remove sprintf() statements in quite a few places.

One of the things you can't prepare for is $this->data becoming $this->request->data, but that should be a search and replace.

One thing I'm not going to enjoy is converting the LdapDataSource to CakePHP 2.0.  Hopefully it shouldn't be a big deal, but it plays an important part of the login process.  Actually, it looks like CakePHP has an LDAPSource, but the conversion it not completed.  That might be a golden opportunity to contribute something back.

No comments:

Post a Comment