Cakephp: isAjax weirdness
Currently, I’m doing a project in CakePHP.
There’s lots to say about cake, here just a quick note, for myself or for anyone in case you are running into the same problem.
Situation: you are using the Ajax helper, which contains a method isAjax()
– returns true
if the request was an Ajax request.
Typically, you use this method inside a view, to conditionally render content for regular/ajax views. Example:
...
if (! $ajax->isAjax()) {
// render general page stuff
}
// render stuff for both regular- and ajax view
...
My problem was that the isAjax()
call never returned true, regardless of ajax/regular request method.
The solution appears to be really simple: you should include the 'RequestHandler'
component in your controller class. This one took me way too long to figure out!
public $components = array (
'Auth',
'Cookie',
'RequestHandler'
);
Update my situation was even worse than described above; I use a redirect between controllers within an Ajax call, something which is officially supported by Cake 1.2. This worked just fine in Safari, but not in Firefox (and Opera) – these browsers would not load the Ajax view but rather the complete page, almost crashing the server by consuming huge amounts of memory.
Read on…
Lots and lots of troubleshooting did not help, after which I tried my last bet: update Cake to the current development build.
And that appeared to be the solution! Not only solves this the Ajax detection problem, the whole application seems to be much more efficient too.
The Cake SVN repository is organised somewhat unconventional. A quick recipe for getting the last development revision…
The repository is rooted at https://svn.cakephp.org/repo/.
There are the common subdirectories, amongst others:
- branches
- tags
- trunk
In this case, Trunk contains the last released (beta) versions, while branches contain, well, the trunk repository.
So in order to get the current HEAD release, you have to get the source from /branches/1.2.x.x.
Command line:
svn co https://svn.cakephp.org/repo/branches/1.2.x.x/
I’m working with revision 6461, highly recommended!
[ratings]