Don’t add a component twice

Last week I spent an annoyingly long time tracking down a bug caused by what was basically a simple typo.

I discovered that my controller redirects (e.g. $this->redirect(array('action'=>'index')); )were failing, but without generating any errors at all (that I could find).

Eventually I tracked down the cause by just taking the whole controller apart and rebuilding it one step at a time – I was including a component (PhpThumb) in my controller but the component was subsequently being included using App:import within a behaviour.

Set CakePHP layouts in app_controller

Fed up of writing $this->layout = ‘admin’ in all the admin methods of your controllers? Me too.

I realised that instead you could automatically set them in the beforeFilter() of your app_controller instead.

function beforeFilter(){

  if(isset($this->params['prefix'])) {
    //read the admin prefix set in core.php
    $admin = Configure::read('Routing.admin'); 

    if($this->params['prefix'] == $admin){
	
      $this->layout = $admin;
	  
    }
  }    
}

If you need to over-ride this, you can just do so as normal in individual methods.

At last the CakePHP books have arrived

I’d like to say it never rains but it pours, but that would be entirely the wrong sentiment, I need something more like good things come to those who wait

Anyway after an epic wait a flurry of CakePHP books have arrived.

I haven’t read any of these books yet, but I from personal experience I would say the Apress books are bound to be good. I have no idea what the books from Packt are like. I have one Manning book which is good but is unfortunately a PDF (and I hate reading PDFs).