Ch-ch-anges C a k e P H P   2 . 0
CakePHP 2.0No more PHP4, PHP 5.2.6+Many re-factored/re-built internal & externalAPI’sNew features!Faster!Updated conventio...
PHP5Use native features:  Exceptions.  Filter ext.  PDO.  SPL.  json_encode()
Conventions
Updated conventions ClassName = FileName.php View/Helper/HtmlHelper.php => HtmlHelper. CamelCased directories. Preparing f...
New loaderApp::import() - Old and busted.App::uses() - New hawtness.
Examples
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);     Classname
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);     Classname                     Package
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);     Classname                     Package      App::uses(‘Cake...
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);     Classname                     Package      App::uses(‘Cake...
ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);     Classname                     Package      App::uses(‘Cake...
PackagesUse App::build() to define paths where anypackage lives.Sensible defaults come built in.Undefined packages default t...
pathsUse App::build() to define paths wherepackages can be found.Can use App::PREPEND and App::APPENDto control insertion o...
Bad things happen
ExceptionsObject::cakeError() is dead.New Exceptions & Exception handlingreplace it.More extensible and configurable handli...
Example 1   <?php 2   // Customize the renderer. 3   Configure::write(Exception, array( 4       handler => ErrorHandler::h...
Built-in exceptionsAll previous Object::cakeError() areexceptions.Improved testing.Suite of HTTP exceptions. Great for err...
HTTP ExceptionsMost common HTTP exceptions are built in.The names are what you would expect.Configured Exception handler wi...
HttpExceptions1 <?php2 public function view($id = null) {3     $this->Tag->id = $id;4     if (!$this->Tag->exists()) {5   ...
Error Configuration Configure used for error/exception settings. Sensible defaults.   Uses Debugger in development   Logs e...
i18n
i18nNo more true.Automatic sprintf()
Examples__(‘Some string’)Always returns.__(‘Some %s’, $value);Does what you want it to.
Unify disparate     apis
Unify & syngergizeHelper, Components, Behaviors & Tasks.Similar, but interacting with each was slightlydifferent.Collectio...
CollectionsLoad objects at runtime.Unload objects.Alias objects.Trigger callbacks.
Loading$Html = $this->Helpers->load(‘Html’, $settings);$this->Html works from that pointforward.The same API exists for co...
Aliasing1 <?php2 public $helpers = array(3     Html => array(4         className => MyHtmlHelper5     )6 );
AliasingOnce an alias is created, that alias is usedeverywhere.With the above controller code, all otherHelpers would use ...
Unload and disable$this->Components->disable(‘Auth’);$this->Components->enable(‘Auth’);$this->Components->unload(‘Auth’);
Callbacks$this->Helpers->trigger(‘beforeRender’, array(&$this));Fires method on all enabled objects.Implement custom hooks...
Console
Console     improvementsColoured output & output levels.Less coupled & easier to test.Better input handling.Generated help...
COLORZSimple formatting to add styles to output.$this->out(‘<warning>Uh oh</warning>’);$this->out(‘<error>Kaboom</error>’)...
Input handlingMore unix-y input handling.Long and short options.Validated options.Boolean options.Required arguments.
Generated helpDefining command structure withConsoleOptionParser means you get help forfree.--help and -h supported by all ...
Request &Response
CakeRequestConsolidates request interrogation logic.$this->request everywhere.Lower memory use by not copying arrayseveryw...
CakeResponseConsolidates response generation.$this->response everywhere.Deprecated methods still around.Controllers can re...
example1 <?php2 class PostsController extends AppController {3     public function simple() {4         return new CakeResp...
Sessions
SessionsSessions were tough in the past.Custom session handlers weren’t on the samefield as core ones.
SessionsCore, App, and Plugin session handlers allwork the same.CakeSessionInterface interface has to beimplemented.Same b...
Using built-ins 1   <?php 2   Configure::write(Session, array( 3       defaults => php 4   )); 5   Configure::write(Sessio...
Using an app/plugin  1   <?php  2   Configure::write(Session, array(  3       defaults => database,  4       handler => ar...
Authorization
AuthComponentSplit into authorization and authenticationadapters.Easier to modify and extend.No magic logins.No magic hash...
Built-insAuthentication     Authorization  Form.              Controller.  HTTP Basic.        Actions (Acl)  HTTP Digest. ...
Logging in 1 <?php 2 class UsersController extends AppController { 3     public function login() { 4         if ($this->re...
Auth ConfigurationWay less, because there is less magic.Easier for beginners to grok.Easier for veterans to customize.
Email
EmailDecomposed into several classes.Email is easily accessible from models &shells.EmailComponent is deprecated, because ...
Simple Example1   <?php2   App::uses(CakeEmail, Network/Email);3   $mail = new CakeEmail();4   $mail->from(mark.story@gmai...
Templates 1   <?php 2   App::uses(CakeEmail, Network/Email); 3   $mail = new CakeEmail(); 4   $mail->from(mark.story@gmail...
Mail TransportsExtracted out as separate classes.3 built-in transports, and you can make yourown.
Testing
TestingPHPUnit is the de-facto standard.SimpleTest is less feature rich, and didn’thave a PHP5 compatible version when wes...
Performance
All winLazy loading everywhere it made sense.  Components, helpers, associated models.Removed most getInstance() methods.N...
Hello World (rps)350.0262.5                                         CakePHP 1.3.11                                        ...
Basic blog (rps)50.037.5                                      CakePHP 1.3.11                                      CakePHP ...
Functions called                                       Hello World     Basic Blog500375250125  0       CakePHP 1.3.11     ...
Questions?
Upcoming SlideShare
Loading in...5
×

Ch ch-changes cake php2

6,010

Published on

Talk given at Cake

Published in: Technology
1 Comment
10 Likes
Statistics
Notes
No Downloads
Views
Total Views
6,010
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
110
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide

Ch ch-changes cake php2

  1. 1. Ch-ch-anges C a k e P H P 2 . 0
  2. 2. CakePHP 2.0No more PHP4, PHP 5.2.6+Many re-factored/re-built internal & externalAPI’sNew features!Faster!Updated conventions.
  3. 3. PHP5Use native features: Exceptions. Filter ext. PDO. SPL. json_encode()
  4. 4. Conventions
  5. 5. Updated conventions ClassName = FileName.php View/Helper/HtmlHelper.php => HtmlHelper. CamelCased directories. Preparing for PHP5.3 and PSR-0
  6. 6. New loaderApp::import() - Old and busted.App::uses() - New hawtness.
  7. 7. Examples
  8. 8. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’);
  9. 9. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’); Classname
  10. 10. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’); Classname Package
  11. 11. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’); Classname Package App::uses(‘CakeEmail’, ‘Network/Email’);
  12. 12. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’); Classname Package App::uses(‘CakeEmail’, ‘Network/Email’); Classname
  13. 13. ExamplesApp::uses(‘AuthComponent’, ‘Controller/Component’); Classname Package App::uses(‘CakeEmail’, ‘Network/Email’); Classname Package
  14. 14. PackagesUse App::build() to define paths where anypackage lives.Sensible defaults come built in.Undefined packages default to checkingapp/Lib
  15. 15. pathsUse App::build() to define paths wherepackages can be found.Can use App::PREPEND and App::APPENDto control insertion order.
  16. 16. Bad things happen
  17. 17. ExceptionsObject::cakeError() is dead.New Exceptions & Exception handlingreplace it.More extensible and configurable handling.
  18. 18. 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 ));
  19. 19. Built-in exceptionsAll previous Object::cakeError() areexceptions.Improved testing.Suite of HTTP exceptions. Great for errorpages.CakeException for development errors.Treated as 500 errors in production.
  20. 20. HTTP ExceptionsMost common HTTP exceptions are built in.The names are what you would expect.Configured Exception handler will get theexceptions and render an error page.
  21. 21. HttpExceptions1 <?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 }
  22. 22. 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.
  23. 23. i18n
  24. 24. i18nNo more true.Automatic sprintf()
  25. 25. Examples__(‘Some string’)Always returns.__(‘Some %s’, $value);Does what you want it to.
  26. 26. Unify disparate apis
  27. 27. Unify & syngergizeHelper, Components, Behaviors & Tasks.Similar, but interacting with each was slightlydifferent.Collection objects unify all of them. HelperCollection, ComponentCollection, TaskCollection, BehaviorCollection
  28. 28. CollectionsLoad objects at runtime.Unload objects.Alias objects.Trigger callbacks.
  29. 29. Loading$Html = $this->Helpers->load(‘Html’, $settings);$this->Html works from that pointforward.The same API exists for components, tasks,behaviors.
  30. 30. Aliasing1 <?php2 public $helpers = array(3 Html => array(4 className => MyHtmlHelper5 )6 );
  31. 31. AliasingOnce an alias is created, that alias is usedeverywhere.With the above controller code, all otherHelpers would use MyHtmlHelper for$this->Html.
  32. 32. Unload and disable$this->Components->disable(‘Auth’);$this->Components->enable(‘Auth’);$this->Components->unload(‘Auth’);
  33. 33. Callbacks$this->Helpers->trigger(‘beforeRender’, array(&$this));Fires method on all enabled objects.Implement custom hooks in base classes.
  34. 34. Console
  35. 35. Console improvementsColoured output & output levels.Less coupled & easier to test.Better input handling.Generated help.XML help. Good for integration with othertools.
  36. 36. COLORZSimple 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);
  37. 37. Input handlingMore unix-y input handling.Long and short options.Validated options.Boolean options.Required arguments.
  38. 38. Generated helpDefining command structure withConsoleOptionParser means you get help forfree.--help and -h supported by all shells.--help xml generates xml help.
  39. 39. Request &Response
  40. 40. CakeRequestConsolidates request interrogation logic.$this->request everywhere.Lower memory use by not copying arrayseverywhere.Backwards compatible read only interfacestill around.
  41. 41. CakeResponseConsolidates response generation.$this->response everywhere.Deprecated methods still around.Controllers can return response objects.
  42. 42. example1 <?php2 class PostsController extends AppController {3 public function simple() {4 return new CakeResponse(array(5 body => Simple response6 ));7 }8 }
  43. 43. Sessions
  44. 44. SessionsSessions were tough in the past.Custom session handlers weren’t on the samefield as core ones.
  45. 45. SessionsCore, App, and Plugin session handlers allwork the same.CakeSessionInterface interface has to beimplemented.Same built-in options exist.
  46. 46. 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 => session12 )13 ));
  47. 47. 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 => true 15 ) 16 ));
  48. 48. Authorization
  49. 49. AuthComponentSplit into authorization and authenticationadapters.Easier to modify and extend.No magic logins.No magic hashing of passwords.
  50. 50. Built-insAuthentication Authorization Form. Controller. HTTP Basic. Actions (Acl) HTTP Digest. CRUD (Acl) custom custom
  51. 51. 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 }
  52. 52. Auth ConfigurationWay less, because there is less magic.Easier for beginners to grok.Easier for veterans to customize.
  53. 53. Email
  54. 54. EmailDecomposed into several classes.Email is easily accessible from models &shells.EmailComponent is deprecated, because itdoesn’t really add anything at this point.
  55. 55. Simple Example1 <?php2 App::uses(CakeEmail, Network/Email);3 $mail = new CakeEmail();4 $mail->from(mark.story@gmail.com, Mark Story)5 ->to(steve@apple.com, Steve Jobs)6 ->subject(Love the new imacs)7 ->send(I <3 you so much.);
  56. 56. Templates 1 <?php 2 App::uses(CakeEmail, Network/Email); 3 $mail = new CakeEmail(); 4 $mail->from(mark.story@gmail.com, Mark Story) 5 ->to(steve@apple.com, Steve Jobs) 6 ->subject(Love the new imacs) 7 ->template(love_you, simple) 8 ->emailFormat(html) 9 ->viewVars($vars)10 ->send();
  57. 57. Mail TransportsExtracted out as separate classes.3 built-in transports, and you can make yourown.
  58. 58. Testing
  59. 59. TestingPHPUnit is the de-facto standard.SimpleTest is less feature rich, and didn’thave a PHP5 compatible version when westarted.Tune in for my talk on PHPUnit.
  60. 60. Performance
  61. 61. All winLazy 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.
  62. 62. Hello World (rps)350.0262.5 CakePHP 1.3.11 CakePHP 2.0-beta175.0 87.5 0 Hello world siege -b -c 20 -r 100 http://localhost/hello/world
  63. 63. Basic blog (rps)50.037.5 CakePHP 1.3.11 CakePHP 2.0-beta25.012.5 0 Basic blog siege -b -c 20 -r 100 http://localhost/posts
  64. 64. Functions called Hello World Basic Blog500375250125 0 CakePHP 1.3.11 CakePHP 2.0-beta Collected using webgrind and xdebug
  65. 65. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×