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:
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
Model
- 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 $namein your model definition. - Model-related database tables: Set
var $useTablein your model definition.
Controller
- 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 thirdparty pieces of code, Cake has no control over them.