CakePHP 2.1 is a fully API compatible upgrade from 2.0. This page outlines the changes and improvements made for 2.1.
These classes are now required to be part of the app directory, as they were removed from the CakePHP core. If you do not already have these classes, you can use the following while upgrading:
// app/View/Helper/AppHelper.php
App::uses('Helper', 'View');
class AppHelper extends Helper {
}
// app/Model/AppModel.php
App::uses('Model', 'Model');
class AppModel extends Model {
}
// app/Controller/AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller {
}
// app/Console/Command/AppShell.php
App::uses('Shell', 'Console');
class AppShell extends Shell {
}
If your application already has these files/classes you don’t need to do anything. Additionally if you were using the core PagesController, you would need to copy this to your app/Controller directory as well.
The default .htaccess files have changed, you should remember to update them or update your webservers URL re-writing scheme to match the changes done in .htaccess
The default exception rendering now includes more detailed stack traces including file excerpts and argument dumps for all functions in the stack.
debug() now uses Debugger internally. This makes it consistent with Debugger, and takes advantage of improvements made there.
A new TestShell has been added. It reduces the typing required to run unit tests, and offers a file path based UI:
./Console/cake test app Model/Post
./Console/cake test app Controller/PostsController
./Console/cake test Plugin View/Helper/MyHelper
The old testsuite shell and its syntax are still available.
Controller::$uses was modified the default value is now true instead of false. Additionally different values are handled slightly differently, but will behave the same in most cases.
- true Will load the default model and merge with AppController.
- An array will load those models and merge with AppController.
- An empty array will not load any models other than those declared in the base class.
- false will not load any models, and will not merge with the base class either.
View::$output is deprecated.
$content_for_layout is deprecated. Use $this->fetch('content'); instead.
$scripts_for_layout is deprecated. Use the following instead:
echo $this->fetch('meta');
echo $this->fetch('css');
echo $this->fetch('script');
$scripts_for_layout is still available, but the view blocks API gives a more extensible & flexible replacement.
The Plugin.view syntax is now available everywhere. You can use this syntax anywhere you reference the name of a view, layout or element.
The $options['plugin'] option for element() is deprecated. You should use Plugin.element_name instead.
Two new view classes have been added to CakePHP. A new JsonView and XmlView allow you to easily generate XML and JSON views. You can learn more about these classes in the section on JSON and XML views
View has a new method allowing you to wrap or ‘extend’ a view/element/layout with another file. See the section on Extending Views for more information on this feature.
The ThemeView class is deprecated in favor of the View class. Simply setting $this->theme = 'MyTheme' will enable theme support, and all custom View classes which extend from ThemeView should extend View.
View blocks are a flexible way to create slots or blocks in your views. Blocks replace $scripts_for_layout with a more robust and flexible API. See the section on Using view blocks for more information.
Two new callbacks have been added to Helpers. Helper::beforeRenderFile() and Helper::afterRenderFile() these new callbacks are fired before/after every view fragment is rendered. This includes elements, layouts and views.