CakePHP - Some good practices

Michał Szajbe

CakePHP - Some good practices

When you write small applications, you don't really need to care about quality of your code. However this is a must in medium or large ones. If you don't spend some time on planning, there is a reasonable chance that you'll end up with an unfinished project. After writing some in Cake, I have some views on how to write good (or not bad, at least!). Big part of what you'll read below is related to software engineering in general.

1. Plan the structure of your application

This is the most important. Analyze requirements, identify objects. It's good to divide application to separable modules and implement and test them one at the time. Connect them after they're tested. Advantages of such approach are obvious - high cohesion, easier implementation and testing, maintainability. It's also easy to assign tasks to programmers, because they can work independently.

2. Implement functionalities with abstraction in mind

Try to write each module of the application in a way that it could be reused in another projects without much changes (preferrably only configuration ones). For example let's consider following situation: you want users of your site to be able to rate objects (photos, comments, whatever...).

database table ratings:

  • id - primary key

  • user_id - id of user that rates an object

  • model - name of the model of an object (Photo, Comment, etc)

  • foreign_key - id of an object

  • rating - actual rating chosed by an user

Such structure lets users rate any object in the system. I'd suggest writing a Behavior that shares a method to write ratings in database. Then to make an object ratable you'd only need to add this behavior to $actsAs property of object's model. Simple and generic.

3. Keep controllers independent of user interface

Controller's action should be implemented in a way, that there is no difference whether they're accessed via AJAX or usual request. It's views (and layouts) that are responsible for user interface. If you want to create rich AJAX application, alter the views. Controllers and their action, if correctly written, may remain unchanged.

4. Move the heart of your application to models

You should place most of business logic of your app in models as the concept Fat model, skinny controller suggests. Often many controllers share models beetwen them and perform the same actions on them. Placing these actions inside models makes it easier to test, debug and change them, because all the changes need to be made in one place only. Similarly, implementation of such mechanisms as caching or logging is much easier to do and maintain. It's less error-prone.

5. Use elements extensively

Many pages of the site display the same elements - nav bars, breadcrumbs, menus, etc. Define them in elements. You can pass variables with renderElement() method so you can customize them anytime. For example: create and edit form are almost the same, the only difference is probably the value of action attribute of form tag. So it's good to make them an element.

Remember that it's the first controllers and models you write that determine your application's structure and the way it develops. In the middle of the work it's hard to change the approach, so you should invest some time in analyzing and planning before you start coding. It will pay off.

Michał Szajbe avatar
Michał Szajbe