We’ve started separating our admin controllers from our public controllers, for a couple of reasons:
-
Separation of concerns
-
Distinction: have our admin URLs display as "http://site.com/admin/…" by convention.
-
Security: separating admin controllers from public controllers simplifies access control.
-
Performance: admin controllers tend to be heavier than public controllers - by separating them, we avoid loading up a bunch of admin-actions when running a public action.
By following a naming convention for admin and public controllers, we end up with controller names like these:
-
UserController
-
UserAdminController
-
PageController
-
PageAdminController
-
etc.
Now for the clever bit. Rather than adding a route for each of the admin controllers, we simply do this:
'urlManager'=>array(
'urlFormat'=>'path', // use "path" format, e.g. "post/show?id=4" rather than "r=post/show&id=4"
'showScriptName'=>false, // do not show "index.php" in URLs
'appendParams' => false, // do not append parameters as name/value pairs (DO NOT CHANGE THIS)
'rules'=>array(
'admin/<controller:\w+>'=>'<controller>Admin/index',
'admin/<controller:\w+>/<id:\d+>'=>'<controller>Admin/view',
'admin/<controller:\w+>/<id:\d+>/<action:\w+>'=>'<controller>Admin/<action>',
'admin/<controller:\w+>/<action:\w+>'=>'<controller>Admin/<action>',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<id:\d+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I’m sure some of you had already figured this out, but this idea was new to me - thought I’d share it