开发者

CakePHP Best Practice: Admin with or without routing

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 fr开发者_如何学编程amework) in this regard?


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.


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'; }


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜