Ketan Patel

CakePHP Tips

cakephp  tips

If you are learning CakePHP then you should know some basic & small snippets that are very useful while developing app with CakePHP.This post contains such small but very useful cakephp tips.

Remove plugin name and controller name from url

If you want to remove plugin and as well as controller name from URL , just add below line on your's routes.php file.

 'plugin' => 'plugin-name', 
 'controller' => 'controller-name', 
 'action' => 'view'

 Render specific view 

You can render a different view than what would conventionally be done. This can be done by calling render() method directly. Once render() is called ,CakePHP will not try to re-render the view:

class PostsController extends AppController {

function your_action() {

This would render app/View/Posts/test_file.ctp instead of app/View/Posts/your_action.ctp

To create a static page

Say for example you want to add about us page.In cakephp you have to create a .ctp file for the page.In this case simply create  about.ctp in the /app/views/pages/ folder.Now you can access the page from

Setting page title for the static page 

Now to give this new page a custom page title copy-paste following code anywhere in 'about.ctp' file:

$this->set("title_for_layout", "My page title");

Create route for the static pages

add below code  in the /app/config/routes.php file.
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Now  you can access it at

To display the schema of the model

If you want to disply the schema of particular model then you just need to add the following single line of code.For example we have “Posts” Controller.


How to set an array index to record ID.

In the below video you will learn how you can set an array index to the fetched record id.
Link :

How to change the extension of .ctp file

In order to change the extension of the .ctp file, add the following line of code in your AppController.php file,

public $ext = '.yourextension';

Notice : If you use atrribute $ext = '.php' but view file has extension .ctp then, cakephp extention .ctp will use by default.

Leverage Browser Caching CakePHP

First of all check your website’s performance here :

It may display a critical error “Leverage browser caching”. Its simple meaning is expiration times are not set for different types of files. Say for example if we have jquery min file and perhaps we will not change it that often, so in this case we can put its expiration time at least 1 month. Doing this means that as soon as the browsers access the page for the very first time, after that during one month accessing the same page they will use the cached version of js file (Note: If the cache was not cleared by the user).

To implement the same scenario in cakephp for Leverage Browser Caching you just  need to add the following code in .htaccess (Note : Please do not delete anything what is already there in the files.) file located in app/webroot.

 ExpiresActive On
 ExpiresByType image/jpg "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType text/x-javascript "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access plus 1 month"
 ExpiresByType image/x-icon "access plus 1 year"
 ExpiresDefault "access plus 2 days"

If  we make any changes in css or js file then we can urge the browser to download the edit css or javascript file adding or editing the version like this. So, these will be considered as new script.js and style.css files by the browser and they will be downloaded, so the changes will take into effect. 

<?php echo $this->Html->script('script.js?v=1');
<?php echo $this->Html->css('style.css?v=1');

Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :