Make your life easier with these five CakePHP Quicktips
Posted on 27/5/08 by Tim Koschützki
Hi guys,
here are five quick CakePHP tips you should consider when you want to make your life easier. Isn't clean code all we want?
1. The prd() convenience function
Do you often find yourself doing the following in your controller, models and views?
Why not wrap it up into a nice function that you can call just like pr() ?
Simple, yet effective! You can just drop that into your bootstrap.php and do this from now on:
One line less to type! I hear many people argueing that you should use debug(). Good call, but it does not provide a parameter that let's you exit out, so you would end up doing something similar anyway. If someone uses anything else, be sure to throw it into the discussion.
2. How to debug your CakePHP emails?
Yeah so you set up a local mailserver in order to test your emails' output? Then had to wait between one and twenty minutes for the email to arrive? That is not KISS is it?
Cake's Email Component has a nice debug mode which you can use. Just set the following in your controller:
The Component stores its debug output as a Session flash Message, however. In order to access the debug information, you would do this:
prd($this->Session->read('Message.email'));
The dump includes all headers, subject and message. The debug output is internally wrapped into a <pre> tag and therefore you have to keep in mind that non-explicit newlines will be shown as such in the debug output. However, they will not be in the email text. Be sure to explicitely insert newlines in the email via
for example.
You could also create a debugEmail() method in the app controller to wrap this Session read call to not always have to remember it.
3. Use elements where possible and make them belong to the controller
In a web application you will find yourself dealing with the same "site elements" time and again. Cake's elements are very handy for that. So why not use the views of a specific model as an element, too, to save code lines? Have a look at this in a fictive /views/posts/index.ctp
<p>Sorry, there are no posts yet!</p>
<?php return; endif; ?>
<?php foreach ($posts as $i => $post): ?>
// big long code to render the post which is duplicated in /views/posts/view.ctp
<?php endforeach; ?>
This looks much better:
<p>Sorry, there are no posts yet!</p>
<?php return; endif; ?>
<?php foreach ($posts as $i => $post): ?>
<?php echo $this->element('../posts/view', array('post' => $post, 'forIndex' => true, 'excerpt' => false)); ?>
<?php endforeach; ?>
You can use that as an element now in the index.ctp view as we did or as a different page. Only controller-independent elements should go into /views/elements. Everything else should stay in the controller-specific folder as view files. Disagree? Share your view!
4. Combine your h1 titles with Cake's page title
Very often people set the page title within their controller, which is bad. The page title is view logic so it should go into the view. For everybody not knowing about this yet, you can do the following in any view file as well:
So let's go a step further and output the page title as the h1 headline for your page in a very semantic line to kill two birds with one stone:
5. Avoid long parameter lists
A method with long parameter lists is always not a nice thing. It takes up space and will most likely need to be wrapped over two lines. How ugly!
Consider this:
$defaults = array(
'assocs' => array(
'types' => array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany')
),
'queries' => array(
'filters' => array(),
'associated' => true,
'describe' => false
),
'schema' => array(),
'log_sql_table' => 'logs'
);
$this->settings = am($defaults, $settings);
}
instead of a function with a long parameter list.
Happy baking!
-- Tim Koschuetzki aka DarkAngelBGE