views:

53

answers:

1

Hi guys,

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 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.

+1  A: 

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.

fire
Thanks fire, that makes sense and that's how i normally lay out an app with a distinct front and back end - on this occasion i've got a bit muddled by the fact that both logged in users and public users can access some of the same pages, so i tried to use the Website_Controller to cope with this. But, I should probably just handle the 2 user types distinctly here, without any crossover in the Website_controller
kenny99
Do the types of static calls I'm making in my Home_Controller make sense in the context of a controller responsible for generating content to be output via the view?
kenny99
Hmm, thinking about this some more and i have a problem because i can't split the admin and public sections up by URL - so i need a global check to see it's a logged in member or not. For example, if a member is accessing the public homepage and is already logged in, then I have to include a check so i know whether the homepage he should see is the public or protected one. This brings me back to square one again!
kenny99