Archive for the ‘CakePHP’ Category

Custom Errors in CakePHP

Monday, March 29th, 2010

I like CakePHP’s error validation quite a lot. I use it in nearly every case because it’s easy and it works. There are times when the standard error validation scheme does not work. Like when you have a field that you want to validate manually in the controller, but still use CakePHP’s error display method. There is no method to simply inject an error into the existing validation system. After a bit of searching I finally found where the error messages are being stored. The error messages are stored on the model instance in the controller, such as $this->User->validationErrors. When the controller renders the view it copies each model’s validationErrors into the view. To add your error it’s as simple as adding another item to that array that matches your field.

For registration that requires a promotional code:

<?php echo $form->input('promo'); ?>

Then after checking the promo code you can set an error message like this:

$this->User->validationErrors['promo'] = 'Invalid promo code';

The promo code isn’t really part of our User model, it’s only used during registration. However, as far as I see you must have the field associated with a model to add that error message. I suppose an alternate option would be to make a Promo model and associate that with your controller, but that seems like too much work.

CakePHP and dompdf autoload error

Tuesday, February 2nd, 2010

I recently tried using dompdf in a CakePHP project to easily generate PDFs from HTML. I ran into a rather obtuse error while trying to render a page and generate a PDF in the same controller action.

Fatal error: require_once() [function.require]: Failed opening required 'vendors/dompdf/include/htmlhelper.cls.php' in vendors/dompdf/dompdf_config.inc.php on line 194

That makes it seem a lot like dompdf is trying to include one of its files and failing. What’s really happening is dompdf is specifying an __autoload function which PHP is attempting to use to load the HtmlHelper class used by CakePHP. dompdf blindly includes the file if it exists or not thinking only its files would be loaded this way. To get around this and load the files normally, simply edit the DOMPDF_autoload function to make sure the file exists before including.

function DOMPDF_autoload($class) {
    $filename = DOMPDF_INC_DIR . "/" . mb_strtolower($class) . ".cls.php";
    if(is_file($filename)) {
        require_once($filename);
    }
}

I found this snippet after much searching over at http://www.dashinteractive.net/dompdf/index.php?v=3278826, hopefully this post will save someone else a few minutes of googling.

Redirecting to requested URL in CakePHP with custom auth controller

Thursday, August 20th, 2009

I recently ran into the problem of having to redirect a user back to their requested url after logging in. In CakePHP you would normally use Auth::redirect() and that handles it for you. However, I had already written a custom authentication function in my app_controller. I simply wanted to figure out which url the user had requested, save that in the session, and send them back there after a successful login. This may seem like a very simple thing to do, but getting your current URL isn’t so easy in CakePHP.

The best solution I found was to use Router::getParams(). This will return you an array of info about the request. The currently request url, relative to you CakePHP install, will be in will be in ['url']['url']. The URL does not have a starting slash, unless the requested page was ‘/’. For example if you have CakePHP installed in http://www.mysite.com/cake_test/ and you request http:/www.mysite.com/cake_test/users/edit/15 the URL will be ‘users/edit/15′. So to add this into my auth controller I simply added:

$request_params = Router::getParams();
$this->Session->write('auth_redirect','/'.$request_params['url']['url']);

Then after a successful login you can simply:
$this->redirect($this->Session->read('auth_redirect'));