Complete CakePHP naming conventions

For details into some aspects take a look at my old "coding standards" summary.

Database

Database tables are plural: "comments" or "user_comments" etc.

I recommend to stick to conventions and underscore + lowercase all table fields: "last_login" (instead of "lastLogin" or even worse "last login") etc.

Models

Model names are singular and camelCased: "Comment or "UserComment"
filename: "comment.php" or "user_comment.php"

Controllers

Model names are plural and camelCased: "Comments or "UserComments"
filename: "comments_controller.php" or "user_comments_controller.php"

Controller actions should be underscored! "function admin_import_from_xml(){}"

Views

Views have an own folder for each controller – plural: "comments" or "user_comments".
Inside are the templates for the specific actions- underscored.
filenames for example: "index.ctp" or "admin_index.ctp" (with prefix admin) or "admin_order_entries.ctp"

Libs

As long as namespaces are not an issue, they might conflict with existing model classes (they have no "Model" appended!) and other cake core classes or even some vendor files.
I recommend to use the controller syntax here:
Name – camelcased: "GoogleTranslateLib"
filename: "google_translate_lib.php"

This way it won’t interfere with any Google class – or "FileLib" won’t interfere with core "File" class.

Behaviors

Name – camelcased: "GeoPlugin"
filename: "geo_plugin.php"

They should not be similar to model names. Otherwise they might interfere with existing models.
So use adjectives, verbs or plural names: "Ratable", "Geocoded", "Configurations", …

Components

Name – camelcased: "GoogleTranslateComponent"
filename: "google_translate.php"

They could interfere with models (and maybe behaviors). So it might make sense to use plural forms or forms that are not likely to be a model name.

Helpers

Name – camelcased: "GoogleHelper"
filename: "google.php"

With the new 1.3 "$this->Helper" style they can now be anything you want. With a little modification of the core, at least.
I like to call my helpers "MyHelper" if there are specific to a controller/module and it is not yet a plugin.
Example:
Conversations controller + Conversation model + MyConversation helper (as it is only needed inside this specific controller).
Other helpers like "GoogleMap" might be used in several controllers, for instance.

Plugins

They should not be similar to controller names. Otherwise they might interfere with existing controllers.
So use adjectives, verbs or singular names: "Rating", "Setup", "Configuration", …

Constants

No matter if they are class constants or global constants, they are always uppercase and underscored:
"MIN_USER_AGE" etc.

Variables

camelBacked: $userComment

The first letter is uppercase if the variable represents an object:
$File = new File();
$GeoPlugin = new GeoPluginLib();

Layout/Script

CSS classes/ids:
I use camelBacked style here, too: <div class="someClass" id="someId"></div>
But thats purely a matter of taste.
I like to use it in IDs because I can easily attach "-x" (the record id) and split it afterwards on "-" to get the record id in JS:
… id="someId-2"…
comes in handy sometimes

Test Cases

Same as the original file – only with ".test" appended to it: "google_translate_lib.test.php" for instance.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.