Welcome to the Cookbook

loading...
Please login to continue

{722} - Cake Conventions

This section has yet to be written, if you have an idea of what to put here please use the links and let us know your suggestion!

{743} - Cake Conventions

Conventions, eh ?

Yes, conventions. According to thefreedictionary:

  • General agreement on or acceptance of certain practices or attitudes: By convention, north is at the top of most maps.
  • A practice or procedure widely observed in a group, especially to facilitate social interaction; a custom: the convention of shaking hands.
  • A widely used and accepted device or technique, as in drama, literature, or painting: the theatrical convention of the aside.
Conventions in cake are what make the magic happen, read it automagic. Needless to say by favorizing convention over configuration, Cake makes your productivity increase to a scary level without any loss to flexibility. Conventions in cake are really simple and intuitive. They were extracted from the best practices good web developers have used throughout the years in the field of web developement.

Filenames

Filenames are underscore. As a general rule, if you have a class MyNiftyClass, then in Cake, its file should be named my_nifty_class.php.

So if you find a snippet you automatically know that:

  • If it's a Controller named KissesAndHugsController, then its filename must be kisses_and_hugs_controller.php (notice _controller in the filename)
  • If it's a Model named OptionValue, then its filename must be option_value.php
  • If it's a Component named MyHandyComponent, then its filename must be my_handy.php(no need for _component in the filename)
  • If it's a Helper named BestHelperEver, then its filename must be best_helper_ever.php

Models

  • Model class names are singular.
  • Model class names are Capitalized for single-word models, and UpperCamelCased for multi-word models.
    • Examples: Person, Monkey, GlassDoor, LineItem, ReallyNiftyThing
  • many-to-many join tables should be named: alphabetically_first_table_plural_alphabetically_second_table_plural ie: tags_users
  • Model filenames use a lower-case underscored syntax.
    • Examples: person.php, monkey.php, glass_door.php, line_item.php, really_nifty_thing.php
  • Database tables related to models also use a lower-case underscored syntax - but they are plural.
    • Examples: people, monkeys, glass_doors, line_items, really_nifty_things

CakePHP naming conventions are meant to streamline code creation and make code more readable. If you find it getting in your way, you can override it.

  • Model name: Set var $name in your model definition.
  • Model-related database tables: Set var $useTable in your model definition.

Controllers

  • Controller class names are plural.
  • Controller class names are Capitalized for single-word controllers, and UpperCamelCased for multi-word controllers. Controller class names also end with 'Controller'.
    • Examples: PeopleController, MonkeysController, GlassDoorsController, LineItemsController, ReallyNiftyThingsController
  • Controller file names use a lower-case underscored syntax. Controller file names also end with '_controller'. So if you have a controller class called PostsController, the controller file name should be posts_controller.php
    • Examples: people_controller.php, monkeys_controller.php, glass_doors_controller.php, line_items_controller.php, really_nifty_things_controller.php
  • For protected member visibility, controller action names should be prepended with '-'.
  • For private member visibility, controller action names should be prepended with '__'.

Views

  • Views are named after actions they display.
  • Name the view file after action name, in lowercase.
    • Examples: PeopleController::worldPeace() expects a view in /app/views/people/world_peace.thtml; MonkeysController::banana() expects a view in /app/views/monkeys/banana.thtml.

You can force an action to render a specific view by calling $this->render('name_of_view_file_without_dot_thtml'); at the end of your action.

Helpers

  • Helper classname is CamelCased and ends in "Helper", the filename is underscored.
    • Example: class MyHelperHelper extends Helper is in /app/views/helpers/my_helper.php.

Include in the controller with var $helpers = array('Html','MyHelper'); in the view you can access with $myHelper->method().

Components

  • Component classname is CamelCased and ends in "Component", the filename is underscored.
    • Example: class MyComponentComponent extends Object is in /app/controllers/components/my_component.php.

Include in the controller with var $components = array('MyComponent'); in the controller you can access with $this->MyComponent->method().

Vendors

Vendors don't follow any convention for obvious reasons: they are third-party pieces of code, Cake has no control over them.

Differences

Lines: 1-91Lines: 1-2
 <title>Cake Conventions</title> <title>Cake Conventions</title>
-<h3>Conventions, eh ?</h3>
<p>Yes, conventions. According to thefreedictionary: </p>
<ul>
<li>General agreement on or acceptance of certain practices or attitudes: By convention, north is at the top of most maps.</li>
<li>A practice or procedure widely observed in a group, especially to facilitate social interaction; a custom: the convention of shaking hands.</li>
<li>A widely used and accepted device or technique, as in drama, literature, or painting: the theatrical convention of the aside.</li>
</ul>
Conventions in cake are what make the magic happen, read it <strong>automagic</strong>. Needless to say by favorizing convention over configuration, Cake makes your productivity increase to a scary level without any loss to flexibility. Conventions in cake are really simple and intuitive.
They were extracted from the best practices good web developers have used throughout the years in the field of web developement.
<h3>Filenames</h3>
<p>Filenames are <strong>unders
core&lt;/strong>. As a general rule, if you have a class <strong>MyNiftyClass</strong>, then in Cake, its file should be named my_nifty_class.php.</p>
<p>So if you find a snipp
et you automatically know that: </p><br /><ul>
<li>If it's a Controll
er named <strong>KissesAndHugsController</strong>, then its filename must be <strong>kisses_and_hugs_controller.php</strong> (notice _controller in the filename)</li>
<li>If it's a Model named <strong>OptionValue</strong>, then its filename must be <strong>option_value.php</strong>
</li>
<li>If it's a Component named <strong>MyHandyComponent</strong>, then its filename must be <strong>my_handy.php</strong>(no need for _component in the filename)</li>
<li>If it's a Helper named <strong>BestHelperEver</strong>, then its filename must be <strong>best_helper_ever.php</strong>
</li>
</ul>
<h3>Models</h3>
<ul>
<li>Model class names are <strong>singular</strong>.</li>
<li>Model class names are Capitalized for single-
word models, and UpperCamelCased for multi-word models.
&l
t;ul&gt;
<li>Exampl
es: Person, Monkey, GlassDoor, LineItem, ReallyNiftyThing</li> /></ul>
</li>
<li>man
y-to-many join tables should be named: alphabetically_first_table_plural_alphabetically_second_table_plural ie: tags_users</li>
<li>Model filenames use a lower-case underscored syntax.
<ul>
<li>Examples: person.php, monkey.php, glass_door.php, line_item.php, really_nifty_thing.php</li>
</ul>
</li>
<li>Database tables related to models also use a lower-case underscored syntax - but they are <strong>plural</strong>.
<ul>
<li>Examples: people, monkeys, glass_doors, line_items, really_nifty_things</li>
</ul>
</li>
</ul>
<p>CakePHP naming con
ventions are meant to streamline code creation and make code more readable. If you find it getting in your way, you can override it.</p>
<ul>
<li>Model name: Set var $name in your model definition.</li>
<li>Model-related database tables: Set var $useTable in your model definition.</li>
</ul>
<
h3>Controllers</h3>
<ul>
<li>Controller cl
ass names are &lt;strong>plural</strong>.</li> />&lt;li>Controller class names are Capitalized for single-word controllers, and UpperCamelCased for multi-word controllers. Controller class names also end with 'Controller'.
<ul>
<li>Exampl
es: PeopleController, MonkeysController, GlassDoorsController, LineItemsController, ReallyNiftyThingsController</li>
</ul>
</li>
<li>Controller file names
use a lower-case underscored syntax. Controller file names also end with '_controller'. So if you have a controller class called PostsController, the controller file name should be posts_controller.php
<ul>
<li>Examples: people_controller.php, mo
nkeys_controller.php, glass_doors_controller.php, line_items_controller.php, really_nifty_things_controller.php</li>
</ul>
</li>
<li>For protecte
d member visibility, controller action names should be prepended with '-'.</li>
<li>For private member vi
sibility, controller action names should be prepended with '__'.</li>
</ul>
<h3>Views</h3>
<ul>
<li>Views are named after actions they display.</li>
<li>Name the view file after action name, in lowercase.
<ul>
<li>Examples: PeopleController::worldPeace() expects a view in <strong>/app/views/people/world_peace.thtml</strong>; Mon
keysController::banana() expects a view in <strong>/app/views/monkeys/banana.thtml</strong>.</li>
</
ul>r /></li>
</ul>
<p>You can force an action to render a
specific view by calling $this-&gt;render('name_of_view_file_without_dot_thtml'); at the end of your action.</p&gt;
<h3&
gt;Helpers&lt;/h3>
<ul>
<l
i>Helper classname is CamelCased and ends in &quot;Helper&quot;, the filename is underscored.
<ul>
<li>Example: class MyHelperHelper extends Helper is in <strong>/app/views/helpers/my_helper.php</strong>.</li>
</ul>
</li>
</ul>
<p>Include in the controller with var $helpers = array('Html','MyHelper'); in the view you can access with $myHelper-&gt;method().</p>
<h3>Components</h3>
<ul>
<li>Component classname is CamelCased and ends in &quot;Component&quot;, the filename is underscored.
<ul>
<li> Example: class MyComponentComponent extends Object is in <strong>/app/controllers/components/my_component.php</strong>.</li>
</ul>
</li>
</ul>
<p>Include in the controller with var $components = array('MyComponent'); in the controller you can access with $this-&gt;MyComponent-&gt;method().</p>
<h3>Vendors</h3>
<p>Vendors don't follow any convention for obvious reasons: they are third-party pieces of code, Cake has no control over them.
</p>
+<p>This section has yet to be written, if you have an idea of what to put here please use the links and let us know your suggestion!</p>