[CakePHP] Alias in the url. Take 2

Some days ago, I posted about having dynamic URL aliases with cake-php 1.1.18. In that post I used the AppError object trapping the missingAction error.

When I was ready for production environment, during tests I wondered that the AppError is ignored when core debug level is set to 0. While I’m asking about the utility of this behavior, I asked in the list and searched for a solution.

One solution could be to take the __construct method from the ErrorHandler, removing the Configure::read() test and paste it into the AppError::__construct() without calling the parent::__construct(). But in this way if some new feature, fixes, etc. is distributed within that method you’ll probably miss it until you monitor each release.

Finally I opted for useing the routes.php file mapping each action and then mapping the “missing action” to the controller::view() action and the manage the code inside it. Here is a PDF with an extract of the code.

Advertisements

[CakePHP] $ajax.observeField and Session lost

Finally even me landed on the AjaxHelper for give a bit of coolnes (;)) to an application of mine. I’m still using 1.1.18 version of CakePHP.

The problem finally was that when I did an ajax request call the session was restored. Asking in the Mailing List the problems seems to be that when an ajax call has been made the browser doesn’t send a correct User-Agent.

Starting from the hint gave me in the answer to my post, I found the solution that was setting the CAKE_SECURITY in app core.php to a level of medium or lower.

[CakePHP] alias in the url

The task was to have “aliases” for url. For example let’s think about a ProjectsController; each project is characterized by a unique id, a unique name and some other attributes. With the cake way you can refer to the projects with /projects/add, /projects/edit/$id, /projects/delete/$id and /projects/view/$id.

I would like to have an alias for the view: call each project even with a url like /projects/$name (friendlier url).

I achieved the task extending the ErrorHandler::missingAction(). Here is the code I used (pdf).

I don’t know if this is the proper way to do this but it’s working.

[CakePHP] disable model queries caching

Again a simple tip. CakePHP by default, in order to optimize performance, has a query caching system. This is a very cool feature but sometimes it may be self-defeating. For example when you do queries for Unit Testing.

In order to disable query caching, it’s enough to set the model cacheQueries variable to false. Setting it to true will re-enable queries caching.

You can disable it for the whole model “life-time” setting it during “design time”

<?php
class Project extends AppModel {
var $name = "Project";
var $customValidate = array("name");
var $cacheQueries = false;
...
?>

or you can temporarily disable it by calling the $this->Model->cacheQueries=false (use your model instead of “Model” 🙂 )

Here is some extract of code (pdf).