views:

307

answers:

5

I'm working on an overhaul of a CakePHP app I built under CakePHP 1.2. I've upgraded to 1.3 and am considering moving away from the admin routing paradigm for my application. I'm finding that some of my controllers are getting extremely large because of duplicate functions for front end and admin. My intuition is that it is much cleaner to just create a set of admin controllers and drop the admin routing all together, but I wanted to get input on what others are doing and what, if any, functionality I'm going to miss out on dropping the routing.

What are considered best practices for a robust CakePHP app (or other MVC framework) in this regard?

A: 

Do not bother with admin routing if it does not fit your scenario. I'm also not using it, admin paths do not fit my app. Duplicating code is totally waste of effort.

You can use ACL rules for fine grained roles, or simply check role (admin flag) in controller's beforeFilter() or in the first line of an action.

I have a Component function checkRole(array()), that is called in the first line of my actions. If the current user does not have the supplied roles, it logs and terminates the request.

sibidiba
A: 

I'd second using ACL/roles for real admin stuff, and probably not using admin routing in production. I sometimes keep a scaffolded (so minimal extra code) admin routing for low-level admin stuff, accessible only to me, but that's probably not wise in a robust production app.

Edit after commentary: It's not optimal, but you may be able to put together something that will appear like you want it in the URLs, and also be organized into folders. I haven't been able to test it yet, but here's the idea:

Create a folder "admin" in your controllers folder and for the users admin, make a users_admin_controller.php controller file. They collapse the folder structure, so you still can't have the same names as your root dir, but you can still separate them into a folder.

This will by default do an /admin_users/add type situation, but that can be tweaked with the second part, some routing:

Router::connect('/admin/users/:action', array('controller'=>'admin_users'))

This would have to be done for each admin section - not ideal, but I can't figure out a better way without modifying Cake code.

cincodenada
So then what do your admin section URLs look like? I feel like this would be a no-brainer, except there is no good way to put controllers in subdirectories that are accessed via /subdir/control/action (e.g. /admin/users/add). Do you just name all your admin controllers with "admin" prepended (e.g. /admin_users/add)?
seth
If using ACL/roles, there are just pages/actions that are either only accessible to admins, or have extra functionality with admins.But what I think you're asking is when using the scaffolding, or with admin sections. There, you can use "admin routing" to have the admin/users/add like you want. See the CakePHP manual:http://book.cakephp.org/view/46/Routes-Configuration#Prefix-Routing-544With that you just have an `admin_add` function in your users controller, which is accessed via `/admin/users/add`, and then a normal `add` function that is accessed via `/users/add`.
cincodenada
I fully understand (and have implemented) ACL/Roles and I'm not using scaffolding. I just have lots of front end / admin section controllers that would be the same. I'd like to put admin controllers in a subdirectory "/admin" and have urls look like "/admin" without using prefixes.
seth
Ah, I see what you're looking for. It looks like you can put controllers in folders, but it basically flattens the directory structure when it goes to do the URLs (https://trac.cakephp.org/ticket/3567). This complicates any attempt to do things fancily. And I don't think the routing is flexible enough to do any fancy tricks like /admin/users/blah -> /admin_users/blah, which would be another workaround if it worked.
cincodenada
I've edited the solution with another idea. It doesn't do the admin/users->admin_users automatically, but it should work, especially if there aren't too many admin sections.
cincodenada
A: 

I've used ACL for my application and I find it much better than using admin routing. Its much easier. If you really want a prefix, you can do that with normal routing.

Nigel
I ended up using ACL + prefix (which actually isn't really a change from my original implementation). I'm still fairly annoyed at how cake handles directory structures and file names and links. It would be ideal if a controller was in an "admin" directory that that would become part of the url path.
seth
+1  A: 

I'd suggest to simply separate front-end application and admin into two separate applications (/app and /admin). Just think of admin as a simple front-end application, doing all the "dirty" work with the database.

By doing so, you'll be able to access your admin using /admin prefix in URL or set DocumentRoot to /admin/webroot and access admin using subdomain (i.e. admin.myapp.com).

To avoid model code duplication, you could put your models into some shared folder (i.e. /vendors/core/models) and add this path to model paths in bootstrap.php files (App::build('models' => array(VENDORS . 'core/models/')) for CakePHP 1.3, $modelPaths = array(VENDORS . 'core/models/') for CakePHP 1.2).

To add more admin or app specific stuff to your models, you could extend your core models in /models, by loading core model and extending it:

App::import('Model', 'CoreModelName');

class CustomCoreModelA extends CoreModelA
{
    function specificMethod() {}
}

That could be made for shared components, behaviors, etc.

ljank
about using two apps, i would agree and is something i have though about. but your putting models in vendors is an ugly hack. instead use APP::build in the one app to point to the model files in another app. there are problems with this especially when it comes to plugins. very slow looking in so many dirs.
dogmatic69
You're right, putting shared models in /vendors dir is ugly and hacky, using other app models dir to lookup for models sounds better. The only problems is the lack of namespaces, that way you'd have to prefix all your admin models. What about performance? Well, you have to choose whether to have clean and maintainable architecture or lightning fast app, but with lots of architectural drawbacks.
ljank
A: 

ive built apps using admin routing and not, and the not version is always a mess. if some of your methods are the same you can do the following.

function add(){
$this->_add();
}

function admin_add(){
$this->_add();
}

function _add(){
... your code ...
}

i would bet its not all your code that is the same, and not using admin routing you will end up with a lot of code doing if(... is admin ...) { echo 'blaa'} else { echo 'foo'; }

dogmatic69