Category Archives: CakePHP

Access beforeFilter set variable in controller (CakePHP)

In a CakePHP application we could define an intermediate abstraction of a controller called AppController. The concept is having a Controller class defined by Cake core, writing controllers for our project and defining app_controller.php file for our common functions repeated in every controller.

In this case we usually use beforeFilter or beforeRender method in AppController in order to define some data to be transferred automatically in every controller. More about app_controller and callback functions.

When a variable is set in beforeFilter, it could be used with it's name later in CakePHP views. For example:


  2. $this->set('products', array('P1' => 'Pizza', 'P2' => 'Water'));


Any view later:


  2. <?php echo $products['P1']; ?>

But that variable could not be accessed directly in a controller function. In this case we could use viewVars array in order to access beforeFilter set variable in other controller method, which solves our problem: 


  2. function test() {
  3. $products = $this->viewVars['products'];
  4. $product_1 = $products['P1'];
  5. }
  6. Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati

Easy AJAX calls in CakePHP

 CakePHP is a great framework for rapid development of web based applications that serves well the principles of DRY (Do not Repeat Yourself). However unlike other web frameworks, CakePHP is not (and probably will not) fully AJAX based (for instance, you could check the list of PHP Ajax frameworks or try some Java/Python implementations such as Google Web Toolkit and Pyjamas). 

In no way the statement above means that you cannot do some flexible and dynamic client-side calls into your CakePHP site. You could freely use plain Javascript in your views, use the JS helper of CakePHP or add some prototype/jQuery library and take advantage of its features. Beyond all that possibilities you could use one more helper of Cake - the AJAX helper. The AJAX helper provides an intuitive interface for maintaining some standard task with only few lines of code.

Some of the methods that you can use with the Ajax helper:

  • link
  • remoteFunction
  • form
  • observeField
  • autoComplete
  • drag & drop

and few more that you could find in the link above. 

The standard 1.2 implementation expects prototype and scriptaculous in order to run properly. However you could easily replace them with jQuery (and jQuery UI) libraries. Some examples on that: Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati

How to hide load time comment for AJAX calls in CakePHP

 CakePHP puts some debug info after page load including the load time of the page. Even though it is an HTML comment line, it does affect returned data for AJAX calls (and not only). 

If we do return JSON, we could set the returned type for json-like and then json_encode the data using the $this->autoRender = false; clause. However, if we expect some specific data (like a boolean yes/no result), we would have problems with the debug feature of CakePHP.

So we could disable the debugging feature of Cake only for AJAX calls adding this statement to the beforeFIlter() action of the app_controller.php:


  1. if ( $this->RequestHandler->isAjax() ) {
  2. Configure::write('debug',0);
  3. } Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati

Motoclub.BG – our new moto project


We delivered Motoclub.BG and now it's available and accessible. The web portal is an online version of a magazine for motorcycle sports and vehicles, ATVs and others. As the bikers' favourite music is rock/metal, breaking news on concerts and live events in clubs are posted there as well.

The portal has few interesting sections for magazine online overview. You could find news and specialised articles, moto calendar with coming events, catalogue with specifications for different brands and so on. Filters and archives provide searching via keywords or month/year ranges. RSS feed on latest news is staying in the left column, next to the invitation to the Facebook group of the website.

The project is CakePHP based with MySQL database behind. I've implemented a few custom solutions on user management and RSS aggregation which I'm going to describe later. Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati