Cake Conventions

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:

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

  1. Model class names are singular.
  2. Model class names are Capitalized for single-word models, and UpperCamelCased for multi-word models.
    1. Examples: Person, Monkey, GlassDoor, LineItem, ReallyNiftyThing
  3. many-to-many join tables should be named: alphabetically_first_table_plural_alphabetically_second_table_plural ie: tags_users
  4. Model filenames use a lower-case underscored syntax.
    1. Examples: person.php, monkey.php, glass_door.php, line_item.php, really_nifty_thing.php
  5. Database tables related to models also use a lower-case underscored syntax – but they are plural.
    1. 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.

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

  1. Controller class names are plural.
  2. Controller class names are Capitalized for single-word controllers, and UpperCamelCased for multi-word controllers. Controller class names also end with ‘Controller’.
    1. Examples: PeopleController, MonkeysController, GlassDoorsController, LineItemsController, ReallyNiftyThingsController
  3. 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
    1. Examples: people_controller.php, monkeys_controller.php, glass_doors_controller.php, line_items_controller.php, really_nifty_things_controller.php
  4. For protected member visibility, controller action names should be prepended with ‘-’.
  5. For private member visibility, controller action names should be prepended with ‘__’.

Views

  1. Views are named after actions they display.
  2. Name the view file after action name, in lowercase.
    1. 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
  1. Helper classname is CamelCased and ends in “Helper”, the filename is underscored.
    1. 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

  1. Component classname is CamelCased and ends in “Component”, the filename is underscored.
    1. 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 thirdparty pieces of code, Cake has no control over them.

Leave a Comment