Help organising controllers logically
I'm working on a site which i'm developing using an MVC structure. My models will represent all of data in the site, but i'm struggling a bit to decide on a good controller structure. The site will allow users to login/register and see personal data on a number of pages, but also still have access to public pages, e.g FAQs, Contact page etc.
This is what I have at the moment...
A Template Controller which handles main template display. The basic template for the site will remain the same whether or not you are logged in.
A main Website Controller which extends the Template Controller and handles basic authentication. If the user is logged in, a User::control_panel() method is called from the constructor and this builds the control panel which will be present throughout the authenticated session. If user is not logged in, then a different view is loaded instead of the control panel, e.g with a login form. All protected/public page related controllers will extend the website controller.
The user homepage has a number of widgets I want to display, which I'm doing via a Home Controller which extends the Website Controller. This controller generates these widgets via the following static calls:
$this->template->content->featured_pet = Pet::featured();
$this->template->content->popular_names = Pet::most_popular();
$this->template->content->owner_map = User::generate_map();
$this->template->content->news = News::snippet();
I suppose the first thing I'm unsure about is if the above static calls to controllers (e.g Pet and User) are ok to remain static - these static methods will return views which are loaded into the main template. This is the way I've done things in the past but I'm curious to find out if this is a sensible approach. Other protected pages for signed in users will be similar to the Home Controller.
Static pages will be handled by a Page Controller which will also extend the Website Controller, so that it will know whether or not the user control panel or login form should be shown on the left hand side of the template. The protected member only pages will not be routed to the Page Controller, this controller will only handle publicly available pages.
One 开发者_JAVA技巧problem I have at the moment, is that if both public and protected pages extend the Website Controller, how do I avoid an infinite loop - for example, the idea is that the website controller should handle authentication then redirect to the requested controller (URL), but this will cause an infinite redirect loop, so i need to come up with a better way of dealing with this.
All in all, does this setup make any sense?! Grateful for any feedback.
I have my controllers laid out like this:
/**
* Global controller
*/
class MY_Controller extends Controller {
function __construct() {
parent::__construct();
// Load templates, modules etc
}
}
/**
* Admin controller
*/
class Admin_Controller extends MY_Controller {
function __construct() {
parent::__construct();
// Check admin is logged in
}
}
/**
* User controller
*/
class User_Controller extends MY_Controller {
function __construct() {
parent::__construct();
// Check user is logged in
}
}
Any user page would extend User_Controller and admin page would extend Admin_Controller that way you can easily put in authentication without many headaches and keep it all seperate.
精彩评论