ch ch-changes cake php2
DESCRIPTION
Talk given at CakeTRANSCRIPT
![Page 1: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/1.jpg)
Ch-ch-angesC a k e P H P 2 . 0
![Page 2: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/2.jpg)
CakePHP 2.0
No more PHP4, PHP 5.2.6+
Many re-factored/re-built internal & external API’s
New features!
Faster!
Updated conventions.
![Page 3: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/3.jpg)
PHP5
Use native features:
Exceptions.
Filter ext.
PDO.
SPL.
json_encode()
![Page 4: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/4.jpg)
Conventions
![Page 5: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/5.jpg)
Updated conventions
ClassName = FileName.php
View/Helper/HtmlHelper.php => HtmlHelper.
CamelCased directories.
Preparing for PHP5.3 and PSR-0
![Page 6: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/6.jpg)
New loader
App::import() - Old and busted.
App::uses() - New hawtness.
![Page 7: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/7.jpg)
Examples
![Page 8: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/8.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
![Page 9: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/9.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
Classname
![Page 10: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/10.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
Classname Package
![Page 11: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/11.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
Classname Package
App::uses(‘CakeEmail’, ‘Network/Email’);
![Page 12: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/12.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
Classname Package
App::uses(‘CakeEmail’, ‘Network/Email’);
Classname
![Page 13: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/13.jpg)
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
Classname Package
App::uses(‘CakeEmail’, ‘Network/Email’);
Classname Package
![Page 14: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/14.jpg)
Packages
Use App::build() to define paths where any package lives.
Sensible defaults come built in.
Undefined packages default to checking app/Lib
![Page 15: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/15.jpg)
paths
Use App::build() to define paths where packages can be found.
Can use App::PREPEND and App::APPEND to control insertion order.
![Page 16: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/16.jpg)
Bad things happen
![Page 17: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/17.jpg)
Exceptions
Object::cakeError() is dead.
New Exceptions & Exception handling replace it.
More extensible and configurable handling.
![Page 18: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/18.jpg)
Example 1 <?php 2 // Customize the renderer. 3 Configure::write('Exception', array( 4 'handler' => 'ErrorHandler::handleException', 5 'renderer' => 'MyCustomExceptionRenderer', 6 'log' => true 7 )); 8 9 // Replace everything.10 Configure::write('Exception', array(11 'handler' => function ($e) {12 echo 'Oh noes';13 }14 ));
![Page 19: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/19.jpg)
Built-in exceptions
All previous Object::cakeError() are exceptions.
Improved testing.
Suite of HTTP exceptions. Great for error pages.
CakeException for development errors. Treated as 500 errors in production.
![Page 20: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/20.jpg)
HTTP Exceptions
Most common HTTP exceptions are built in.
The names are what you would expect.
Configured Exception handler will get the exceptions and render an error page.
![Page 21: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/21.jpg)
HttpExceptions
1 <?php2 public function view($id = null) {3 $this->Tag->id = $id;4 if (!$this->Tag->exists()) {5 throw new NotFoundException(__('Invalid tag'));6 }7 $this->set('tag', $this->Tag->read(null, $id));8 }
![Page 22: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/22.jpg)
Error Configuration
Configure used for error/exception settings.
Sensible defaults.
Uses Debugger in development
Logs errors in production.
You can change out the whole thing.
![Page 23: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/23.jpg)
i18n
![Page 24: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/24.jpg)
i18n
No more true.
Automatic sprintf()
![Page 25: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/25.jpg)
Examples
__(‘Some string’) Always returns.
__(‘Some %s’, $value); Does what you want it to.
![Page 26: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/26.jpg)
Unify disparate apis
![Page 27: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/27.jpg)
Unify & syngergize
Helper, Components, Behaviors & Tasks.
Similar, but interacting with each was slightly different.
Collection objects unify all of them.
HelperCollection, ComponentCollection, TaskCollection, BehaviorCollection
![Page 28: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/28.jpg)
Collections
Load objects at runtime.
Unload objects.
Alias objects.
Trigger callbacks.
![Page 29: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/29.jpg)
Loading
$Html = $this->Helpers->load(‘Html’, $settings);
$this->Html works from that point forward.
The same API exists for components, tasks, behaviors.
![Page 30: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/30.jpg)
Aliasing
1 <?php2 public $helpers = array(3 'Html' => array(4 'className' => 'MyHtmlHelper'5 )6 );
![Page 31: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/31.jpg)
Aliasing
Once an alias is created, that alias is used everywhere.
With the above controller code, all other Helpers would use MyHtmlHelper for $this->Html.
![Page 32: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/32.jpg)
Unload and disable
$this->Components->disable(‘Auth’);
$this->Components->enable(‘Auth’);
$this->Components->unload(‘Auth’);
![Page 33: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/33.jpg)
Callbacks
$this->Helpers->trigger(‘beforeRender’, array(&$this));
Fires method on all enabled objects.
Implement custom hooks in base classes.
![Page 34: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/34.jpg)
Console
![Page 35: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/35.jpg)
Console improvements
Coloured output & output levels.
Less coupled & easier to test.
Better input handling.
Generated help.
XML help. Good for integration with other tools.
![Page 36: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/36.jpg)
COLORZ
Simple formatting to add styles to output.
$this->out(‘<warning>Uh oh</warning>’);
$this->out(‘<error>Kaboom</error>’);
$this->out(‘<info>Hi</info>’, 1, Shell::VERBOSE);
![Page 37: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/37.jpg)
Input handling
More unix-y input handling.
Long and short options.
Validated options.
Boolean options.
Required arguments.
![Page 38: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/38.jpg)
Generated help
Defining command structure with ConsoleOptionParser means you get help for free.
--help and -h supported by all shells.
--help xml generates xml help.
![Page 39: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/39.jpg)
Request & Response
![Page 40: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/40.jpg)
CakeRequest
Consolidates request interrogation logic.
$this->request everywhere.
Lower memory use by not copying arrays everywhere.
Backwards compatible read only interface still around.
![Page 41: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/41.jpg)
CakeResponse
Consolidates response generation.
$this->response everywhere.
Deprecated methods still around.
Controllers can return response objects.
![Page 42: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/42.jpg)
example
1 <?php2 class PostsController extends AppController {3 public function simple() {4 return new CakeResponse(array(5 'body' => 'Simple response'6 ));7 }8 }
![Page 43: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/43.jpg)
Sessions
![Page 44: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/44.jpg)
Sessions
Sessions were tough in the past.
Custom session handlers weren’t on the same field as core ones.
![Page 45: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/45.jpg)
Sessions
Core, App, and Plugin session handlers all work the same.
CakeSessionInterface interface has to be implemented.
Same built-in options exist.
![Page 46: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/46.jpg)
Using built-ins 1 <?php 2 Configure::write('Session', array( 3 'defaults' => 'php' 4 )); 5 Configure::write('Session', array( 6 'defaults' => 'database' 7 )); 8 Configure::write('Session', array( 9 'defaults' => 'cache',10 'handler' => array(11 'config' => 'session'12 )13 ));
![Page 47: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/47.jpg)
Using an app/plugin 1 <?php 2 Configure::write('Session', array( 3 'defaults' => 'database', 4 'handler' => array( 5 'engine' => 'Custom' 6 ) 7 )); 8 Configure::write('Session', array( 9 'defaults' => 'php',10 'handler' => array(11 'engine' => 'Plugin.SessionHandler'12 ),13 'ini' => array(14 'session.cookie_httponly' => true15 )16 ));
![Page 48: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/48.jpg)
Authorization
![Page 49: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/49.jpg)
AuthComponent
Split into authorization and authentication adapters.
Easier to modify and extend.
No magic logins.
No magic hashing of passwords.
![Page 50: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/50.jpg)
Built-insAuthentication
Form.
HTTP Basic.
HTTP Digest.
custom
Authorization
Controller.
Actions (Acl)
CRUD (Acl)
custom
![Page 51: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/51.jpg)
Logging in
1 <?php 2 class UsersController extends AppController { 3 public function login() { 4 if ($this->request->is('post')) { 5 if ($this->Auth->login()) { 6 $this->Session->setFlash('Welcome back!'); 7 $this->redirect($this->Auth->redirect()); 8 } else { 9 $this->Session->setFlash('Login credentials were invalid.');10 }11 }12 }13 }
![Page 52: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/52.jpg)
Auth Configuration
Way less, because there is less magic.
Easier for beginners to grok.
Easier for veterans to customize.
![Page 53: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/53.jpg)
![Page 54: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/54.jpg)
Decomposed into several classes.
Email is easily accessible from models & shells.
EmailComponent is deprecated, because it doesn’t really add anything at this point.
![Page 55: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/55.jpg)
Simple Example
1 <?php2 App::uses('CakeEmail', 'Network/Email');3 $mail = new CakeEmail();4 $mail->from('[email protected]', 'Mark Story')5 ->to('[email protected]', 'Steve Jobs')6 ->subject('Love the new imacs')7 ->send('I <3 you so much.');
![Page 56: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/56.jpg)
Templates
1 <?php 2 App::uses('CakeEmail', 'Network/Email'); 3 $mail = new CakeEmail(); 4 $mail->from('[email protected]', 'Mark Story') 5 ->to('[email protected]', 'Steve Jobs') 6 ->subject('Love the new imacs') 7 ->template('love_you', 'simple') 8 ->emailFormat('html') 9 ->viewVars($vars)10 ->send();
![Page 57: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/57.jpg)
Mail Transports
Extracted out as separate classes.
3 built-in transports, and you can make your own.
![Page 58: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/58.jpg)
Testing
![Page 59: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/59.jpg)
Testing
PHPUnit is the de-facto standard.
SimpleTest is less feature rich, and didn’t have a PHP5 compatible version when we started.
Tune in for my talk on PHPUnit.
![Page 60: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/60.jpg)
Performance
![Page 61: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/61.jpg)
All win
Lazy loading everywhere it made sense.
Components, helpers, associated models.
Removed most getInstance() methods.
New conventions mean less Inflector calls.
Streamlined dispatch cycle.
Persistent dbo cache.
![Page 62: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/62.jpg)
Hello World (rps)
0
87.5
175.0
262.5
350.0
Hello world
CakePHP 1.3.11CakePHP 2.0-beta
siege -b -c 20 -r 100 http://localhost/hello/world
![Page 63: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/63.jpg)
Basic blog (rps)
0
12.5
25.0
37.5
50.0
Basic blog
CakePHP 1.3.11CakePHP 2.0-beta
siege -b -c 20 -r 100 http://localhost/posts
![Page 64: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/64.jpg)
Functions called
0
125
250
375
500
CakePHP 1.3.11 CakePHP 2.0-beta
Hello World Basic Blog
Collected using webgrind and xdebug
![Page 65: Ch ch-changes cake php2](https://reader031.vdocuments.us/reader031/viewer/2022013003/55669b23d8b42a78708b522b/html5/thumbnails/65.jpg)
Questions?