Best place for user data

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Best place for user data

Post 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)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Best place for user data

Post 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.
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Best place for user data

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Best place for user data

Post 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.
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Best place for user data

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Best place for user data

Post 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
  }
}
(#10850)
Post Reply