cakephp tips and tricks

When i first picked up cakephp i found it a bit of a struggle to find the right kind of documentation. Don’t get me wrong they have the api and the bakery which has alot of information but for some reason just basic explanations and ideas on how to use the framework seemed to take me a long time to discover. This post is more of a way for me to document some of the things i have found but i also hope others find it useful. Naming conventions Database tables should be named as plurals e.g comments Model files should be named as the singular so for the table comments the model file would be called comment Controllers should be plural so the filename would be comments_controller.php and in the browser would be /comments/ Creating a model without a table Quite alot of the time when i have been experimenting with cakephp i found i would create a model and not need a table for it, cake however isists you have a table to match. You can stop this by adding var $useTable = false; to the model file. If you haven’t come across the need for this you may be asking yourself why you would really need this. Well essentially i tend to use a model file for say a contact page and its form to define the fields the validation.

<?php
class Contact extends AppModel {
    	var $name = 'contact';
    	var $useTable = false;
    	var $_schema = array(
            'name'		=>	array('type'=>'string', 'length'=>100),
            'email'		=>	array('type'=>'string', 'length'=>255),
            'details'	=>	array('type'=>'text')
        );
    	var $validate = array(
    		'name' => array(
    			'rule'=>array('minLength', 1),
    			'message'=>'Name is required' ),
    		'email' => array(
    			'rule'=>'email',
    			'message'=>'Must be a valid email address' ),
    		'details' => array(
    			'rule'=>array('minLength', 1),
    			'message'=>'Feedback is required' )
    	);
}
?>

Paths If you want to link to an image or url in any website that has mod_rewrite urls like /category/blah/view/2 you can’t use relative links otherwise the browser will try to load your image or pages from the wrong location e.g http://mywebsite.com/category/blah/view/2/images/test.jpg instead of http://mywebsite.com/images/test.jpg. Use the cakephp helpers where ever possible as they will put the full paths in for you. In any other situation prepend the path with $this->webroot

<?php
echo $html->image('/images/test.jpg');
echo $html->link('click here',"/weblink",array(),false,false);
echo $html->link($html->image('/images/test.jpg'),"/weblink",array(),false,false);
?>
<img src="<?php echo $this->webroot; ?>images/test.jpg" />

Web Pages If you have simple web pages that don’t require a database or much php then you can use the cakephp pages sections for this. Simply create a file in the “appviewspages” folder, for example aboutus.ctp and place the page content inside it, then all you need to do is visit your website address with the pages/aboutus on the end. e.g http://www.mysite.com/pages/aboutus Quite often i will do this and then realise i need to put in place some specific code in a controller. For example i will create a contactus page then realise i need to put some php code in to handle submitting the form. Which means i have to give it a controller a view folder and model etc. So it is a good idea to think about exactly what that page is going to be doing before making it. What are plugins, components, behaviours and helpers? Components are used to aid and assist controllers in performing their business logic. They extend Object and do not have direct access to the model. Typical usage is in Authentication, Filtering, and Rendering controls. Examples: EmailComponent, AuthComponent, ImageComponent. Helpers extend the functionality of the view and enable you to keep the templates clean. In the class you can place some display logic or organize some view specific code. Examples include AjaxHelper, TreeHelper, FormHelper. Plugins are mini-applications, but are easier to distribute and just drop into the main application. They extend the functionality of the application by providing a self-contained MVC for a specific part of your application. Examples could include a Gallery, a Blog, or a Forum. Behaviors help the Model handle data. Examples include List, Tree, etc.

When i first picked up cakephp i found it a bit of a struggle to find the right kind of documentation. Don’t get me wrong they have the api and the bakery which has alot of information but for some reason just basic explanations and ideas on how to use the framework seemed to take me a long time to discover. This post is more of a way for me to document some of the things i have found but i also hope others find it useful. Naming conventions Database tables should be named as plurals e.g comments Model files should be named as the singular so for the table comments the model file would be called comment Controllers should be plural so the filename would be comments_controller.php and in the browser would be /comments/ Creating a model without a table Quite alot of the time when i have been experimenting with cakephp i found i would create a model and not need a table for it, cake however isists you have a table to match. You can stop this by adding var $useTable = false; to the model file. If you haven’t come across the need for this you may be asking yourself why you would really need this. Well essentially i tend to use a model file for say a contact page and its form to define the fields the validation.

Comments

    Comments are currently closed