Migrating to cakephp 2.0 tips and changes
Cakephp has released 2.0 version with lot of changes in API, and nice features. The highly sought ORM capability is also added in this version, while I was migrating from my old 1.3.x app to cake 2.0 faced lots of dificulties and thought to share with you peoples. This is a quick note, more verbose information can be found at
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html
1. Rename all controllers pizza_controller.php becomes PizzaController.php same as classs name
2. Rename All components for example my_mail.php becomes MyMailComponent.php
# Rename all helpers in view directory to match for example country.php becomes CountryHelper.php.
3. Move AppController (former app_controller) from app/ directory to app/Controller/ directory.
Also capitalize all model class for example user.php becomes User.php
4. In views
- Use $this->Html->link() instead of $html->link()
- Use $this->Form-> instead of $form->
- Use $this->Session-> instead of $session->
- Use $this->Paginator-> intead of $paginator ->
- For JavaScript inclusion use $this->Html->script(“”) instead of $javascript->link()
NOTE: Cakephp 2.0 has some built in folders under Views like Scanffold, Elements, Emails etc similar to those if want to capitalize view folders for example dont make users => Users, then make sure that Controllers are same name $name = “Users” otherwise they will not work in unix boxes where folder path app/Views/users/ are treated differently than app/Views/Users.
# In Helpers
If you have custom helpers under “Views/Helpers” directory that loads other helpers for example $Html or $Form there will be small change in construct process for example $html = new HtmlHelper() becomes $html = new HtmlHelper($this->_View)
5. In Controller
- Using model find method $this->Student->find(array(“id” => 123)) will not work use find(“first”, array(“id” => 123)).
- If you have tried to modify $this->data like $this->data["xyz"] = “abc” inside controller method this will throw error [Indirect modification of overloaded property ... has no effect] instead you need to do $this->request->data["xyz"] = “abc”.
- In controllers for example AppController Helper inclusion for view should be carefully stated for example var $helpers = array(‘ ‘Js’, ’Html’, ’Form) etc javascript Helper is now called Js helper and html is Html.’
# Authentication
- Cake 1.3.x was automatically checking if user has correctly entered username/password inside your login() method of users_controller but in cake 2 we need to manually call $this->Auth->login() this returns boolean value based on successful login or failure.
# Saving forms
When submitting forms from views to controllers Model->save() method check for !empty($this->request->data) instead of !empty($this->data)
# File uploads
- When submitting multipart-form-data for attaching files from views to controllers, the file data cannot be accessed via $this->data variable rather they are stored into $this->params["data"]
6. Others
- All component files should be extended by Component Class instead of Object
Some precautions
- Do not overwrite routes.php file under your app/Config folder.
Tips
You can use Dreamweaver’s excellent find and replace tool, it works for a single file or recursively on a entire directory. or Netbeans also has Edit -> Replace in project

