Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 framework) in this regard?

like image 462
seth Avatar asked Nov 06 '22 11:11

seth


2 Answers

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.

like image 144
ljank Avatar answered Nov 11 '22 03:11

ljank


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

like image 33
dogmatic69 Avatar answered Nov 11 '22 02:11

dogmatic69