Cake PHP Admin Routing

Comments

First things first: KISS.

As a developer who (in the early stages of CakePHP’s development) constantly reinvented the wheel with features already available through the framework itself, I am now so cautious of doing so it’s almost an abhorrence rather than a discipline. However, I feel that this particular obvious solution to a problem is worth sharing:

The Problem

I've got an admin prefix set up in my Cake application and I have a custom layout that I want to use instead of the default layout. I don't really want to specify it in all the admin functions within each controller, is there a way to do it within app_controller.php?

Yes:

function beforeRender() {
 if(!empty($this->params['prefix']) && $this->params['prefix']=='admin') {
  $this->layoutPath = 'admin';
 }
}

It's really simple. $this->action is the current action—lower case underscore (eg. the_current_action)— as a string. For a single word prefixed route (admin, manage, moderate, etc.) it is easily searched. You could opt for $this->layout = 'admin_layout' if you like, but I find that using $this->layoutPath affords a bit of freedom in future should i want to have multiple admin layouts and then I can have a specific controller function line that doesn't need to worry about the beforeRender function overriding anything.

Labels: , ,

Really bored of Facebook

Comments

I don't really have any profound discourse for you here; I have this underwhelming apathy for it now. You know what? Bring back Geocities, bring back some creativity. The most annoying thing is that people have access to a plethora of real web services and nobody is looking at them. That’s why I spend all my time trying to push real world (hah) links to Facebook instead of relying on the incredibly sinister like button.

Read more »

Back to Safari

Comments

Yup, I've officially switched back to Safari. Chrome was a great ride but it left me missing some core features of OS X that I didn't want to do without. Since moving to Chrome, I've accepted life without some of the more hacky SIMBL plugins that I had and so my return to Safari means that I can wave bye bye bye bye bye suckers to:

  • Saft
  • Glims

And I'm saying hello to a number of SIMBL plugins that are free:

And Greasekit that is forever my best friend forever. I've got some plans to work on a special Greasekit plugin too.

Also, I'm no longer blocking ads. No more bastardising the web. If I don't want to see it I'll just do what I learnt to do years ago before ad blockers existed: Let my eyes glaze over and carry on…

A fresh start

Comments

I decided to start from fresh. No more handrolling content management systems and tacking on my blog. No more reliance on hosting bills and root for something that was a fickle pastime. But now I'm back and ready to rock with a blog hosted on Blogger

Read more »

Recent Posts