NAMING FILES AND DESIGNING THE DATABASE by DerekSchouman

VIEWS: 19 PAGES: 3

									                                   Naming cakePHP files
                                    By: Joyce Lusunike.

   1.      Naming Controllers

Names of database tables as well as controllers are lowercase and in plural form.
Thus, the Orders controller would be named orders_controller.php. It will be stored in the
app/ controller folder.
Naming the controller class in the file and how to extend the AppController object.

<?
class OrdersController extends AppController {
}
?>

   2.      Naming Models

Models are named like controllers except they represent interacting with one instance of
the database table at a time, so they will take on a singular, not plural, form.
The corresponding model for the orders table would be named order.php and be stored in
the app/models folder. Notice that for models, you do not append an underscore plus
model to the file name.

How to name the model class in the file and how to extend the AppModel object.

<?
class Order extends AppModel {
 }
?>

   3.      Naming Views

When the controller renders views, Cake will automatically look for a file with the same
name as the action. Views correspond to actions contained in the controller script and are
housed in a folder named after the controller.

The first step in creating a view to be used as output for a controller script is to create the
folder in app/views that matches with the name of the controller. View folders are
lowercase and plural just like the naming convention for database tables. For the
orders_controller.php file, you would create the folder app/views/orders.

   4.      Naming More Than One Word in the Name

To name a database table that uses more than one word, you can use more than one word
by separating each word with an underscore. Therefore a table, then, containing special
orders would be named something like special_orders, not specialorders.
Example:

Type         File Name                        Class Name         Directory Where
                                                                 Stored
Model      special_order.php             SpecialOrder            app/models
Controller special_orders_controller.php SpecialOrdersController app/controllers
View       list.ctp                                              app/views/special_orders


   5.      Naming Other Cake Resources

Cake divides up other important resources in much the same way that it does with
models, views, and controllers.

   A. Behaviors

When interacting with the database, sometimes the model will need to perform more
complex processes than simply adding, updating, or deleting records.
Behaviors are stored in the app/models/behaviors directory and can be named anything
following the same rules for naming helper files and components. They must have the
.php extension.

Behavior class names are set with the following syntax:

<?
class SpecialBehavior extends ModelBehavior {
}
?>

    B. Component
If more than one controller needed to use the same actions, then you might create a
component.

If you were to create an e-mail component of your own, you could name the file
email.php and place it in the app/controllers/components directory.
If you were to create an e-mail component of your own.

<?
class EmailComponent extends Object {
 }
?>
Notice that the name of the class object is camel cased just as in the controller, and it
extends the class Object.
       C.      Helpers

Helpers provide functionality that is used across multiple views.

Because helpers are set apart for use in the views, they are stored in the app/views/helpers
directory. The file name convention is just like components, and they carry the .php
extension.

When naming the helper class object, the following syntax is used:

<?
class EmailHelper extends Helper {
}
?>

            D. Elements

An element contains presentation output that can be pulled into multiple view files.
Anything contained in the element will be displayed in the view depending on where in
the view’s markup the element is called.

Elements are named like controller actions and view files and are stored in the app/views/
elements directory.

Notice:      Helpers and elements differ in that helpers work with view logic,
whereas elements are more like chunks of HTML that repeat throughout the views.

For example: A menu bar would likely require less logic (maybe a couple of variables)
and would not contain a series of functions to be rendered.
As an element, the menu bar would work well because multiple views would need to use
it. Creating pie charts or graphs, however, would require several processes and more
logic and therefore would be better suited as a helper than as an element.

            E. Layouts

Many web sites maintain the same overall design from page to page with only specific
areas changing content depending on the page. Rather than store in each view file this
overall design, you can create a layout file to be used by the entire application. Layouts
are stored in the app/views/layouts directory.
A layout’s file name can be anything but must be lowercase and have the .ctp extension.

Important:
Avoid naming tables in the database that might conflict with a name of a Cake
framework element. An example might be naming a table views or controllers.

								
To top