Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CakePHP 2.0 - PHP Matsuri 2011

4,470 views

Published on

My presentation from the PHP Matsuri 2011 conference. This presentation discusses the changes in CakePHP 2.0 and goes into more detail on some of the cooler features that PHP 5 allows us, and some of the internal restructuring.

Published in: Technology
  • Be the first to comment

CakePHP 2.0 - PHP Matsuri 2011

  1. 1. CakePHP 新バージョン 2.0
  2. 2. Graham Weldon• PHP developer 11 years• CakePHP core contributor 4 years• Visiting Japan for 3 years
  3. 3. CandyCanehttp://my.candycane.jp• Recently helping with CandyCane• Project managed by Ando• Lots of good fixes and updates made recently• Good fun working with Japanese developers• Check it out!
  4. 4. Learning Japanese• I am learning Japanese• You can speak “very basic” Japanese with me• But I will have lots of problems understanding• Please forgive me for any mistakes• I can write Japanese better than I can speak it :-)
  5. 5. CakeFest Conference• Manchester, UK• 4 days!• ~100 attendees• People from many countries• Lots of learning• Lots of drinking
  6. 6. Overview
  7. 7. Major Points• No more PHP4• Refactored throughout• New Features• Faster• New Conventions
  8. 8. PHP5• 5.2.6+ (Including 5.3)• Exceptions• PDO Datasources• SPL• Filters• Support for anonymous functions
  9. 9. Upgrade Console• Make upgrading simpler• Do all the tedious work for you• Automate as much as possible• Its not perfect
  10. 10. Conventions
  11. 11. File Naming• MyClass => my_class.php
  12. 12. File Naming• MyClass => my_class.php
  13. 13. File Naming• MyClass => my_class.php• MyClass => MyClass.php• CamelCased Directories• “Namespace-ish” separation• Preparing for PHP 5.3 and PSR-0
  14. 14. File Naming• Singular directory names• Model/Behavior/MyBehavior.php• View/Helper/MyHelper.php
  15. 15. Moved App* Classes• Model/AppModel.php• Controller/AppController.php
  16. 16. New Class Loader• Goodbye to App::import()• Say Hello to App::uses()• Similar functionality• Looks like PHP 5.3 use keyword
  17. 17. App::uses Example
  18. 18. App::uses Example App::uses( ClassName, Package/SubPackage );
  19. 19. App::uses Example App::uses( ClassName, Package/SubPackage );
  20. 20. App::uses Example App::uses( ClassName, Package/SubPackage );
  21. 21. Configuring Packages• Use App::build() to define where packages are located• Defaults to app/Lib• Core provides defaults
  22. 22. App::build()• App::PREPEND• App::APPEND• Control your insertion order• Greater flexibility
  23. 23. Exceptions and Errors
  24. 24. cakeError()• No more cakeError()• Replaced • Exceptions • Exception Handlers• Much better!
  25. 25. Benefits• Extensible error handling• Common sense default exceptions• Configurable handling• PHP 5.3 awesomeness (if you want)
  26. 26. Custom Rendering
  27. 27. PHP 5.3?
  28. 28. We’re web developers• HTTP Exceptions• Named as you would expect• CakeException for development errors • Shows as Error 500 in production
  29. 29. HTTP Exceptions• All the common HTTP Errors are built in• Renderer handles display and output
  30. 30. Example
  31. 31. Internationalization
  32. 32. I18n Shortcut• Simpler• Returns by default• Automatic sprintf() functionality
  33. 33. Return by default• CakePHP 1.x
  34. 34. Return by default• CakePHP 1.x• CakePHP 2+
  35. 35. sprintf() built in• CakePHP 1.x
  36. 36. sprintf() built in• CakePHP 1.x• CakePHP 2+
  37. 37. Collections
  38. 38. CakePHP 1.x• Previously all extensions worked differently • Helpers • Behaviors • Components • Tasks• No common interface / API
  39. 39. CakePHP 2.x• Collection objects to manage them• HelperCollection, TaskCollection, BehaviorCollection, ComponentCollection• Similar functionality, same API• Easy to use and extend
  40. 40. Responsibilities• Load objects when required• Unload objects• Alias objects• Manage callbacks
  41. 41. Object Loading• $this->Html now available• The same API exists for other collections: • Behaviors • Components • Tasks
  42. 42. Aliasing• Handy for providing custom functionality• Used to be annoying to “override” defaults• Possible, but not an elegant solution
  43. 43. CakePHP 1.x
  44. 44. CakePHP 2.x
  45. 45. Global usage• Once an alias is defined, its used everywhere• Anything else accessing $this->Auth would use CustomAuthComponent
  46. 46. More control with Collections• Unload• Disable• Enable
  47. 47. Console
  48. 48. Console Changes• Console now has a home • app/Console• Coloured output• Better input handling• Generated help• Various output formats
  49. 49. Colour• Simple markup to colourise your output • Warning • Error • Info • Question • ... and more
  50. 50. Simple markup
  51. 51. Colour
  52. 52. Auto-Generated Help• Writing documentation sucks• Using the ConsoleOptionParser gives you help for free• Simple to use• Two birds with one stone
  53. 53. ConsoleOptionParser
  54. 54. Request &Response
  55. 55. CakeRequest• All request information in a single place• Use $this->request everywhere• Memory use is lower • No more copies of copies of copies of arrays all over the place• Backwards compatible interface
  56. 56. CakeResponse• Again, consolidate logic to a single location• Use $this->response everywhere• Backwards compatibility provided
  57. 57. Sessions
  58. 58. Sessions• Remove duplicated code (DRY)• Shouldn’t be always included (1.3 fix)• Modelise data handling• Access anywhere• Allow simple customisation• Separate Caching, Database, etc.
  59. 59. Sessions SessionHandlerDatabase Cache PHP
  60. 60. Sessions SessionHandlerDatabase Cache PHP
  61. 61. Sessions SessionHandlerDatabase Cache PHP My Custom Engine
  62. 62. Sessions SessionHandlerDatabase Cache PHP Cake Session My Custom Engine Handler Interface
  63. 63. AuthenticationAuthorization
  64. 64. AuthComponent AuthComponentAuthentication Authorization
  65. 65. AuthComponent• No more magic logins• No more magic hashing• Easier to understand the workflow• Easier to extend• Easier to modify
  66. 66. Authentication• Form• HTTP Basic• HTTP Digest• Custom
  67. 67. Authorization• Controller• Action (ACL)• CRUD (ACL)• Custom
  68. 68. Example Login
  69. 69. Email
  70. 70. EmailComponent• No longer a component• Set of separate classes• Easily used from Models and Consoles• Component is deprecated
  71. 71. Email Example
  72. 72. Email Example In Models!
  73. 73. Unit Testing
  74. 74. Unit Testing• PHPUnit is the de-facto standard for PHP• SimpleTest has less features• SimpleTest was not PHP5 ready when we started• PHPUnit offers greater features• More flexibility in testing
  75. 75. Performance
  76. 76. Performance• All round increases in performance • Lazy Loading • New Conventions => Less Inflector • Simpler dispatch • Persistent DBO cache
  77. 77. Benchmark “Hello World”Benchmarks courtesy of Mark Story
  78. 78. Benchmark “Basic Blog”Benchmarks courtesy of Mark Story
  79. 79. Resources & Links
  80. 80. CakePHP 2 Documentation• Book for CakePHP 2 • http://book.cakephp.org/2.0/ja/ • We need help for Japanese! • Hiromi helping
  81. 81. CakePHP 2 API• API is already available • http://api20.cakephp.org• Updated every time a build succeeds• Automated + up to date
  82. 82. CakePHP 2Continuous Integration• We are using Jenkins• Automated builds and reporting• Better quality framework• Consistent testing• http://ci.cakephp.org
  83. 83. 質問はありますか?
  84. 84. ありがとう

×