Page 1 of 1

Best place for user data

Posted: Mon Mar 23, 2009 1:08 pm
by matthijs
With many web applications you're dealing with users who log in and then do something with their account and their data.

Persisting the user can be done by use of a session. But I have been wondering what the best way is to convey that user id into the rest of the app. I have a MVC setup with Frontcontroller, controllers with actions, models, views and templates.

To give an example. You have the user_id in the session after he's logged in. Now he's going to insert a new item. The model for that item needs that user_id. Now does the controller get the user_id from the session, put it inside the data array that is being posted with the form and then all that data goes to the model to be validated and inserted?

Or does the controller put the user_id inside the form even before it's being posted (as a hidden input field)?

Or does the model itself ask the Authenticator (or however it's called) for the user_id? (and in case if it's not there, throw an exception)

Re: Best place for user data

Posted: Mon Mar 23, 2009 2:09 pm
by Christopher
I don't think the Controller should be doing any of those things. I usually have a User Model that I pass to the View. The the View can access the data thorough a clear interface. The User Model can either be session only object used for Access Control or be a gateway to the database as well.

Re: Best place for user data

Posted: Mon Mar 23, 2009 2:26 pm
by matthijs
Not sure I understand completely what you mean. So you would do something like

Code: Select all

 
class widgetController {
  function add($locator){
    $model = $this->load()->model('Widgets');
    $UserSession = $locator->get('UserSession'); // this is the usermodel/session
    $view = $this->load()->view('WidgetView');
    $view->set('User', $UserSession); // pass it to the View
 
    // .. process logic
    if($form->isValid($request){
        // ..save
    }
  }
 
}
 
But then the question remains how the View passes the user data (say the user_id) to the request

Re: Best place for user data

Posted: Mon Mar 23, 2009 2:36 pm
by Christopher
Yes, although the View could also load/get those object itself. I find that if I give Model objects to the View that it makes changes much easier later. I try to avoid having the Controller give the View any actual data. Keep the contract between the View and Model. The Controller just hooks things together.

Re: Best place for user data

Posted: Mon Mar 23, 2009 2:58 pm
by matthijs
Aha, so something like

Code: Select all

 
class widgetController {
  function add($locator){
    $model = $this->load()->model('Widgets');
    $view = $this->load()->view('WidgetView');
 
    // .. process logic
    if($form->isValid($request){
        // ..save
    }
  }
}
class widgetView(){
  function addform(){
    $user = $this->request->get('UserSession');
    $template = $this->load()->template('WidgetAddForm');
    $template->set('User', $user);
    // ...
  }
}
 
and then the template could get the userId and put it in a hidden input field, so that it ends up in the request data handled by the controller and model.

Re: Best place for user data

Posted: Mon Mar 23, 2009 4:22 pm
by Christopher
I think you can do something like this:

Code: Select all

 
class widgetController {
  function add($locator){
    $model = $this->load()->model('Widgets');
    $view = $this->load()->view('WidgetView', array('model'=>$model));
 
    // .. process logic
    if($form->isValid($request){
        // ..save
    }
  }
}
class widgetView(){
  protected $user;
  function __construct($locator) {
    $this->user = $locator->get('User');
  }
 
  function addform(){
    $template = $this->load()->template('WidgetAddForm');
    $template->set('User', $this->user);
    $this->setRenderer($template);   // rendering the View renders this template object
  }
}