Saturday 13 December 2014

Bad CakePHP Habits & How to Rectify Them!

As a programmer we are always learning, so it is important to follow the best practices and adjust to them.

CakePHP Conventions
There are actually CakePHP coding conventions which should be followed. I will highlight a few things,

1) Control Structures.  So often you see programmers get this wrong, and in even some cases bring practices for other coding languages.  CakePHP expects the following syntax:

if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}

In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.  So this means that the following is incorrect:

if ($foo) {
$bar = true;
if ($action) {
$to = false;
}

The indentation needs to line up correctly.

I often hear programmers say “but I am too busy to make the code neat….” My response is – “trust me, neat code will stand the test of time”.  Writing code which isn’t readable will be a nightmare to come back to if you need to make a change in a few months.

Fat Models, Skinny Controllers

Good CakePHP code will have the logic in the model files. This takes a bit to get used to, but once mastered there is no looking back!  A controller file should be used for what it is intended for in the MVC pattern – controlling!  So use your controller file to handle user actions, while let the code logic go in the model file.



The default add function is as follows:

public function add() {
if ($this->request->is(‘post’)) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__(‘Your post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
}
$this->Session->setFlash(__(‘Unable to add your post.’));
}
}

This controller action is fine for a simple add, but what would happen if you wanted to do things such as send an email to the admin when a post was added, or update another model association when a post was added.  This is additional logic, but this logic shouldn’t go into our controller file.

Instead we would write a function for this in our App.php model, perhaps something like this:

public function addPost($data = array(), $emailAdmin = true) {
$this->create();
$this->save($data);
// update any other tables
// send the email to the admin user
if ($emailAdmin) {
}
// if all is successful
return true;
}

This would then result in a small change to the controller action as follows:

public function add() {
if ($this->request->is(‘post’)) {
if ($this->Post->addPost($this->request->data)) {
$this->Session->setFlash(__(‘Your post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
}
$this->Session->setFlash(__(‘Unable to add your post.’));
}
}

As you can see, the new action is actually one less line, because the $this->Post->create() has been moved to the model file.
This is a perfect, everyday example of where moving logic to the model file is a good idea – and it certainly makes for a much cleaner code base!

Source: CodeWebber

No comments:

Post a Comment