From The Blog

CakePHP: generate .po localization files using Shell

12 October 2013, in


Using the bake command, cakephp will read all your source files and it will extract all the text to translate. So it's very important that you write the string in the i18n function and don't pass just variables. For example to extract the text, using the command line go to you app directory, and execute the i18n command:

$ cd /var/www/mysite/app
$ ./Console/cake i18n

You'll see:

Post a Comment

CakePHP joining tables

28 December 2012, in

In many cases when we want to formulate complex queries, that need joins between tables, we can still add joins on the fly by adding it to the call of find

$students_of_grade = $this->User->find('list',array(

Post a Comment

CakePHP: change default model for a controller

30 November 2012, in

By default a CakePHP controller uses a model with singularized name, for example a controller with name Students will use by default a model named Student,

But if you want or have to change the default model for a controller you can set it using

$modelClass member attribute

for example for a controller named ClassesController and you can't name the model class with name "Class" because it is a keyword in PHP, and you decided to change the model name to Classroom

Class ClassesController extends AppController {

$modelClass = 'Classroom';


Post a Comment

CakePHP: Custom routes and pagination

3 November 2012, in

In CakePHP it is pretty easy to change the way that users reach to a specific action to another beautiful URL

for ex:

you may have a page that contains category details, and users reach to it via:

controller is "categories", action is "view", and it takes 1 parameter id

and you may want to have another form of URLs like

Post a Comment

CakePHP: Get Client's IP Address

27 October 2012, in

in CakePHP 1.x

Through the RequestHandler Component:


in CakePHP 2.x

RequestHandler::getClientIp() is deprecated, so you can use the CakeRequest object



of course you can still use the server variable   $_SERVER['REMOTE_ADDR']

Post a Comment

CakePHP max number of fetched row on pagination

12 October 2012, in

By default, the maximum number of fetched rows on pagination is set to 100 rows, whatever number of rows higher than it you assign it to the "limit", property, it will not affect, but you can adjust it as part of the pagination options:

public $paginate = array(
    // other keys here.
    'maxLimit' => 10

If the request's limit param is greater than this value, it will be reduced to the maxLimit value.

Post a Comment

CakePHP and MySQL tinyint datatype

21 September 2012, in

I observed that CakePHP treats tinyint fields as boolean (true/false) , but after investigation found that:

if you set the data type for a field to tinyint with length of 1, then CakePHP will treat it as boolean.

So I changed it to tinyint(2),

That's weird about CakePHP

Post a Comment

Parsing XML easily in CakePHP 2

2 June 2012, in

It is very easy to parse XML in CakePHP , here is an example in CakePHP v2

// import XML Utility class
App::uses('Xml', 'Utility');

// your XML file's location
$xml1 = Xml::build('http://url-of-XML');

$xml2 = Xml::build('http://http://url-of-XML', array('return' => 'simplexml'));
// $xml now is a instance of SimpleXMLElement

Post a Comment

CakePHP 2 aggressive Security - continued

29 February 2012, in

CakePHP 2.0 has another aggressive security which is shown clearly when trying to make an ajax request to an action via method POST, it fires security error that says "Request is blackholed due to auth".

The Solution to disable this aggressive validation for this action to set security attribute $validatePost to false.


Post a Comment

CakePHP 2 Aggressive Security

20 February 2012, in

CakePHP has had CSRF protection since 1.2. For 2.0 the existing CSRF has a new more paranoid mode, and is its own standalone feature.

By default a new CSRF token is generated for each request, and each token can only be used one. If a token is used twice, it will be blackholed. Sometimes, this behaviour is not desirable, as it can create issues with single page applications. You can toggle on longer, multi-use tokens by setting csrfUseOnce to false. This can be done in the components array, or in the beforeFilter of your controller:

Post a Comment