views:

385

answers:

2

Hello Guys. I'm investigating using Kohana for my next project. The site will consist of user registration (and hence user profiles) where users will have certain privileges. The site will also have an admin section where administrators can go to say block a user or delete a post or look at usage statistics for example. A good comparison site would be a multi-user blog, where each blogger depending on her/his permissions can post/edit/delete blogs...just as an example.

Firstly, I'm not sure about how to set up the controller/view structure in order to separate the admin section from the front facing site. I'm using Kohana 3, so I was thinking of a controller structure like so: application/classes/controller/front (front facing)...and application/classes/controller/admin (for administrative section).

Or I notice you may be able to use the Route class to set up routes, so I could set up an "admin" route. for example: www.example.com/admin will lead to the admin logon screen. www.example.com ---> front controller.

As well, can I somehow separate the "Admin" views and controllers from the "front facing" views and controllers like divide them up based on folder structure? Any help is very much appreciated.

Thank you.

+3  A: 

You could have a separate application folder for the admin and front-end:

  • application
    • classes
      • controller
      • model
    • views
  • admin_application
    • classes
      • controller
      • model
    • views

This approach would allow you to customise each bootstrap environment individually, and separates the various files nicely. However, due to this separation you will need to structure shared code as modules, to allow the functionality to be shared across the two apps. You could just duplicate the code of course, but that would wrong now, wouldn't it! ;)

Another approach would be to have admin subfolders within each folder of a single application:

  • application
    • classes
      • controller
        • admin
      • model
        • admin
    • views
      • admin

This approach leaves files a little more intermixed, and might make things harder to maintain (depending on your perspective), but it's certainly easier to implement. One advantage of this approach is that you can create a /public_html/admin folder and protect it using .htaccess (you'll need to add a copy of the normal index.php file too). Then whenever any http://yourdomain.com/admin requests are made, the .htaccess file will kick-in and protect your admin application at the webserver level. Plus, the request will automatically route to the /admin subfolders within the various folders, so you've also got less work to do when it comes to routing.

Both situations would use Kohana's (awesome) routing mechanisms to handle which requests went where, and each is as secure as the other from an application access point of view. I've assumed you're using KO3 by the way...

EDIT
Actually, you are able to .htaccess protect the admin app if you use the first method too. You'd just need to adapt the /admin/index.php file to point to the admin app.

MatW
Thanks for your clear answer MatW. I will check out the second approach. Approach 1 looks great, but I'm not too sure how modules work at this moment. Thanks a ton!
berto77
Hi MatW, I decided to stop being a wimp and try out the first folder structure. I have everything set up. But now, do I create a separate bootstrap.php file in 'admin_application'? and how can I tell my application to use this bootstrap when it goes to http://mydomain.com/admin or http://admin.mydomain.com? Thanks a lot
berto77
ok, I think I will make two 'index.php' files. 1.site.php and 2.admin.php...put both of those in document root. also create another bootstrap.php file and place in 'admin_application'. then use .htaccess to route to the correct application (admin_application or application)...not sure, but I'll try. Help appreciated.
berto77
From a new KO3 install - copy your app folder, call it 'admin' or whatever, change the bootstrap base_url to '/admin'. Create a new folder in '/public_html/admin', copy across index.php and .htaccess. In index.php, edit the app, module, and system vars to point to the new admin app. In .htaccess edit the RewriteBase variable to '/admin'. That's all from memory, so there may be a few gotchas I've forgotten, but if you run into trouble, check out Kohana's forums. I can't remember the name of the thread (otherise I'd post it) but they have covered this topic before, and are a friendly bunch.
MatW
A: 

My approach would be similar to the first one but then for each module I would create a admin controller and a frontend controller - All my admin controllers would inherit from an abstract admin controller that would have the authentication in the before method - or something like this.

David
Good points. Thanks-
berto77