PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/en/appendices/new-features-in-cakephp-2-0.rst

https://github.com/MontBlanc-Sucks/docs
ReStructuredText | 312 lines | 229 code | 83 blank | 0 comment | 0 complexity | b653dda8689be73200b068b0da3364b4 MD5 | raw file
  1. New Features in CakePHP 2.0
  2. ###########################
  3. Models
  4. ======
  5. The model construction process has been made lighter. Model associations are
  6. now lazy loaded, applications with lots of models and associations will see
  7. great time reductions in the bootstrap process.
  8. Now models won't require a database connection in the construction process.
  9. The database will be accessed for the first time only when a find operation is
  10. issued or information for one of the columns is required.
  11. View
  12. ====
  13. View::$output
  14. -------------
  15. View will now always have the last rendered content (view or layout) accessible
  16. through ``$this->output``. In helpers you can use ``$this->_View->output``. Modifying
  17. this property will change the content that comes out of the view rendering.
  18. Helpers
  19. =======
  20. HtmlHelper
  21. ----------
  22. * ``getCrumbList()`` Creates breadcrumb links wrapped in ``<li>`` elements.
  23. See `#856 <http://cakephp.lighthouseapp.com/projects/42648/tickets/856>`_.
  24. * ``loadConfig()`` has moved from :php:class:`Helper` to :php:class:`HtmlHelper`
  25. class. This method now uses the new reader classes (see 2.0 :php:class:`Configure`)
  26. to load your config file. As an option you can pass the path as second parameter
  27. (``app/Config`` is default). To simplify, you can set the configuration file
  28. (and the reader) in ``Controller::$helpers`` (example below) to load on helper
  29. constructor. In configuration file you can set the below keys:
  30. * ``tags`` Should be an array with key value;
  31. * ``minimizedAttributes`` Should be a list;
  32. * ``docTypes`` Should be an array with key value;
  33. * ``attributeFormat`` Should be a string;
  34. * ``minimizedAttributeFormat`` Should be a string.
  35. Example of how to set configuration file on controller::
  36. <?php
  37. public $helpers = array(
  38. 'Html' => array(
  39. 'configFile' => array('config_file', 'php') // Option one: an array with filename and reader name
  40. 'configFile' => 'config_file' // Option two: a string with filename. The PhpReader will be used
  41. )
  42. );
  43. FormHelper
  44. ----------
  45. * :php:class:`FormHelper` now supports all HTML5 input types and custom input
  46. types. Just use the input type you want as the method on the helper. For
  47. example ``range()`` would create an input with type = range.
  48. * ``postLink()`` and ``postButton()`` Creates link/button to
  49. access some page using HTTP method POST. With this, in your controller you can
  50. avoid some action, like delete, to be accessed by GET method.
  51. * ``select()`` with multiple = checkbox, now treats the ``'id'``
  52. attribute as a prefix for all the generated options.
  53. Libs
  54. ====
  55. CakeRequest
  56. -----------
  57. :php:class:`CakeRequest` is a new class introduced in 2.0. It encapsulates
  58. commonly used request introspection methods and replaces the params array with a
  59. more useful object. Read more about :php:class:`CakeRequest`.
  60. CakeResponse
  61. ------------
  62. :php:class:`CakeResponse` is a new class introduced in 2.0. It encapsulates
  63. commonly used methods and properties in the HTTP response your application
  64. generates. It consolidates several features in CakePHP. Read more about
  65. :php:class:`CakeResponse`.
  66. CakeSession, SessionComponent
  67. -----------------------------
  68. :php:class:`CakeSession` and the :php:class:`SessionComponent` have had a number
  69. of changes, see the session section for more information.
  70. Router
  71. ------
  72. Routes can return full urls
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. Route objects can now return full urls, and :php:class:`Router` will not further
  75. modify them beyond adding the query string and fragment elements. For example
  76. this could be used to create routes to handle subdomains, or enabling https/http
  77. flags. An example of a route class that supports subdomains would be::
  78. <?php
  79. class SubdomainRoute extends CakeRoute {
  80. public function match($params) {
  81. $subdomain = isset($params['subdomain']) ? $params['subdomain'] : null;
  82. unset($params['subdomain']);
  83. $path = parent::match($params);
  84. if ($subdomain) {
  85. $path = 'http://' . $subdomain . '.localhost' . $path;
  86. }
  87. return $path;
  88. }
  89. }
  90. When creating links you could do the following to make links pointing at other
  91. subdomains.
  92. ::
  93. <?php
  94. echo $this->Html->link(
  95. 'Other domain',
  96. array('subdomain' => 'test', 'controller' => 'posts', 'action' => 'add')
  97. );
  98. The above would create a link with http://test.localhost/posts/add as the url.
  99. Xml
  100. ---
  101. :php:class:`Xml` has had a number of changes. Read more about
  102. :doc:`/core-utility-libraries/xml` class.
  103. New Lib features
  104. ================
  105. Configure readers
  106. -----------------
  107. :php:class:`Configure` can now be configured to load configuration files from a
  108. variety of sources and formats. The :doc:`/development/configuration` section
  109. contains more information about the changes made to configure.
  110. :php:meth:`Configure::read()` without any arguments allows you to read all
  111. values from configure, instead of just the debug value.
  112. Error and exception handling
  113. ----------------------------
  114. CakePHP 2.0 has had :doc:`/development/exceptions` and :doc:`/development/errors`
  115. handling rebuilt, to be more flexible and give more power to developers.
  116. String::wrap()
  117. --------------
  118. :php:meth:`String::wrap()` was added to help make fixed width formatting of
  119. text easier. It's used in Shells whenever you use :php:meth:`Shell::wrapText()`.
  120. debug()
  121. -------
  122. :php:func:`debug()` no longer outputs html in the console. Instead it makes
  123. output like the following::
  124. ########## DEBUG ##########
  125. Array
  126. (
  127. [0] => test
  128. )
  129. ###########################
  130. This should improve readability of ``debug()`` on the command line.
  131. Components
  132. ==========
  133. Components received a similar treatment to helpers and behaviors,
  134. :php:class:`Component` is now the base class for components. Read more about the
  135. component changes.
  136. RequestHandler
  137. --------------
  138. :php:class:`RequestHandler` was heavily refactored due to the introduction of
  139. :php:class:`CakeRequest`. These changes allowed for some new features to be
  140. introduced as well.
  141. Automatic parsing of Accept headers
  142. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  143. If a client sends a single Accept mime type that matches one of the extensions
  144. enabled in :php:class`Router`, :php:class:`RequestHandler` will treat it the
  145. same an extension. This expands CakePHP's support for REST style endpoints. To
  146. use this feature start off by enabling extensions in ``app/Config/routes.php``
  147. ::
  148. <?php
  149. Router::parseExtensions('json', 'xml');
  150. Once you have created layouts and views for your extensions, you will be able to
  151. visit a url like posts/view/1 and send Accept: ``application/json`` in the
  152. headers to receive the json version of that url.
  153. CookieComponent
  154. ---------------
  155. :php:class:`CookieComponent` now supports http only cookies. You can enable
  156. their use by setting ``$this->Cookie->httpOnly = true;``. Having http only
  157. cookies will make them inaccessible from the browser.
  158. Security Component CSRF separation
  159. ----------------------------------
  160. CakePHP has had CSRF protection since 1.2. For 2.0 the existing CSRF has a new
  161. more paranoid mode, and is its own standalone feature. In the past CSRF features
  162. were coupled with form tampering safe-guards. Developers often disabled
  163. validatePost in order to do dynamic forms, disabling the CSRF protection at the
  164. same time. For 2.0 CSRF checking has been separated from form tampering giving
  165. you greater control.
  166. For more information see :ref:`security-csrf`
  167. Controller
  168. ==========
  169. Controllers now have access to request and response objects. You can read more
  170. about these objects on their specific pages.
  171. Console
  172. =======
  173. The console for CakePHP 2.0 was almost entirely rebuilt. Several new features as
  174. well as some backwards incompatible changes were made. Read more about console
  175. changes.
  176. Pagination
  177. ==========
  178. Pagination now provides a default maxLimit for pagination at 100.
  179. This limit can be overridden with the paginate variable on Controller.
  180. ::
  181. <?php
  182. $this->paginate = array('maxLimit' => 1000);
  183. This default is provided to prevent user URL manipulation causing excessive
  184. strain on the database for subsequent requests, where a user would edit the
  185. 'limit' parameter to a very large number.
  186. Aliasing
  187. ========
  188. You can now alias helpers, components and behaviors to use your class instead of
  189. a different one. This means that you can very easily make a ``MyHtml`` helper
  190. and not need to replace every instance of ``$this->Html`` in your views. To do
  191. this, pass the 'className' key along with your class, like you would with
  192. models.
  193. ::
  194. <?php
  195. public $helpers = array(
  196. 'Html' => array(
  197. 'className' => 'MyHtml'
  198. )
  199. );
  200. Similarly, you can alias components for use in your controllers.
  201. ::
  202. <?php
  203. public $components = array(
  204. 'Email' => array(
  205. 'className' => 'QueueEmailer'
  206. )
  207. );
  208. Calls to the Email component would call the QueueEmailer component instead.
  209. Finally, you can alias behaviors as well.
  210. ::
  211. <?php
  212. public $actsAs = array(
  213. 'Containable' => array(
  214. 'className' => 'SuperContainable'
  215. )
  216. );
  217. Because of the way 2.0 utilizes collections and shares them across the
  218. application, any classes you alias will be used throughout your application.
  219. Whenever your application tries to access the alias, it will access your class.
  220. For instance, when we aliased the Html helper in the example above, any helpers
  221. that use the Html helper or elements that load the Html helper, will use MyHtml
  222. instead.
  223. ConnectionManager
  224. =================
  225. A new method :php:meth:`ConnectionManager::drop()` was added to allow
  226. removing connections at runtime.
  227. .. meta::
  228. :title lang=en: New Features in CakePHP 2.0
  229. :keywords lang=en: time reductions,doctypes,model construction,key value,option one,database connection,content view,configuration file,constructor,great time,array,new features,bootstrap process,elements,new models