Fun with CakePHP Admin Routing

Fun with CakePHP Admin Routing

If you develop with CakePHP I am sure you are familiar with its admin routing feature. It’s a nice way to separate your app into a user frontend, and an administrator backend.

By enabling admin routing in the config you can then access admin specific methods of your controllers by going to a url like this: /admin/controller/action. You just prefix your url with “admin” and CakePHP takes care of the rest. For example, the url /admin/users/edit/5 will run the method called admin_edit in your users controller.

I have been working on a client project that requires the application to have more than just an admin and front end interface for users, it needs an additional user level. The users are split across 3 groups, admin, agent, and merchant. My first thought was that I could just sprinkle a bunch of if/else statements into my views and controllers to determine the group permissions and load different elements but quickly threw that out as it just seemed way too messy. It needs to be clean, and flexible. What if we add a new user type later on?

Stepping away from the computer for a bit and just thinking about anything but this project I remembered the state design pattern. It seemed to be the answer to my problem. The program would change based on the user type, an admin would have access to only admin functions, agents can only access agent functions, etc. The functions would change based on the user type. An admin could look at all documents, while agents could only see theirs, and so on. The problem here was that I needed to have admin routing for 3 different user types, and I couldn’t do that from the config file.

The answer came when I started reading the CakePHP documentation for routing and came across the entry for Prefix Routing. CakePHP 1.2 has some pretty flexible and powerful routing features and allows you to define multiple prefix routes. You don’t need to bother setting admin routes in your config as you can just define them in the routes config.

Router::connect('/admin', array('controller' => 'pages''action' => 'index''admin' => true));
Router::connect('/profiles/:controller/:action/*', array('prefix' => 'profiles''profiles' => true)); 

This wouldn’t be CakePHP without a couple gotchas. You must use the HTML helper to create links, which I do anyways. You also need to make some adjustments to your forms, as they ignore the routing prefix.

<?php echo $form->create('User', array('url'=>$this->here));?> 

I didn’t want to use ACL. Its cumbersome, confusing, complicated to setup, and is overkill for what I need. This is just a simple role based app. With the Auth component, and Prefix Routing, I am able to solve almost all of the applications requirements for user management. I wonder though, if there is a better way to do this? I am curious as to how you would solve the problem.

Posted on Dec 16, 2009 | ∞ Permalink | Posted in: News & Articles

blog comments powered by Disqus