Alligator Belly           Gadgets and Technology


CakePHP Naming Conventions

Posted in CakePHP on the March 22nd, 2007

CakePHP is a rapid development framework to speed up PHP development. But for the rapid development to be really rapid you need to follow some rules. The most important rule being the naming conventions. Although it’s pretty straight forward, I had some hiccups here and there. So hopefully this will help someone from losing their hair.

First step in Development: Database Design:
All the tables in the database have to be plural and underscored. For example customers or order_details.
CakePHP expects the primary key attribute in the database to be “id” but some prefer the primary key to be table_name_singular_id which will also work as long as you set the $primaryKey variable in the model definition.

Then the Model:
The file name has to be singular and underscored. For example customer.php or order_detail.php.
The model class name has to be singular and camelCased. For example Customer or OrderDetail.

Once the model is defined, the actions are defined in the Controller:
The file name has to be plural and underscored and followed by a “_controller.php”. For example customers_controller.php or order_details_controller.php.
The class name has to be plural and CamelCased and must be followed by a “Controller”. For example CustomersController or OrderDetailsController.

Lastly the View:
The file name has be to the action name (defined in the controller, ex: add, view, show, get_info, etc). The file has to be placed under the controller name directory in the views folder. So the CustomerController with a add action will require the view file to be placed in views/customers/add.thtml and similarly a get_details action in the OrderDetailsController will require the view to be in views/orderdetails/get_details.thtml.

Following the above naming convention will get you baking your CakePHP app in no time.

Leave a Reply


Site Maintained by Mutual Image