Yii – Separation of admin controllers from public controllers

There are a number of posts and Wiki’s on this subject and I love all the discussions and frays that you get on the Yii Framework forum.

One of the popular methods is to move your Admin functions into a module, but for me this can lead to confusion and duplication, maybe with views and controller functions … but for many it works.

Another popular way, suggested in the Yii Application Cookbook,also listed here, is to divide the application into two separate applications and then use the config in your admin application to also include models from your frontend application.

Method 3

I’ve just started using a method based on using the URLManager and I find it gives a complete logical separation but with a physical integration, ie: ALL your files remain in one application space.

We start with a structure like this:-

        frontend
           index.php
           assets/
           themes/
        protected/
                config/
                        frontend.php
                        backend.php
                components/
                controllers/
                models/
                views/
        backend/
                index.php
                assets/
                themes/

And then we use the URL manager to block access to admin functions in the frontend.

'urlManager'=>array(
                    'urlFormat'=>'path',
                    'showScriptName' => false,
                    'rules'=>array(
                        ''=>'post/view',
                        '<model:.*?>/<action:.*?>'=>'site/error',
                        '...'
                  )
               ),
</action:.*?></model:.*?>

And then I also quite like to obscure the admin functions in the backend using something like this ..
(ps: I know that security through obscurity is not a solution on its own, but it doesn’t hurt as an extra layer) .

     'mySecretAdmin/<model:.*?>'=>'<model>/admin',
     '<model:.*?>/admin'=>'site/error'</model:.*?></model></model:.*?>

As a subnote, if my frontend is relatively simple, I quite like to use specific rules in the URL manager and exclude anything else, for example; in a blog environment the only actions allowable in the frontend have to do with displaying posts and categories, plus maybe one or two other functions …

      'search'=>'post/search',
      <name:.*?>'enquiries'=>'site/enquiry',
      ...
      // Lastly, anything else can go to the post controller view action and let it try and sort it out ....
      'post/<action:.*><controller:w+><action:w+><id:d+>' =>'post/view',
</id:d+></action:w+></controller:w+></action:.*></name:.*?>

I like this way, as to me, it separates the physical organisation from the logical, which is handled by the URL manager, and this seems the right place to do this…

Let’s Start a Project!

Contact Me