User
, context of MVC, is a domain object. However the session is a form of storage medium (just like cache, db or file-system). When you need to store data from User
instance there, you use some type of data mapper to do it.
$user = $this->domainObjectFactory->build('user');
$user->setName('Korben')
->setSurname('Dallas');
if ( some_condition )
{
$mapper = $this->dataMapperFactory->create('session');
$mapper->store($user);
}
This code should provide an extremely simplified example for interaction between session and user.
Where do I add the user class?
As a domain object, the User
instances should be used inside services and initialized using factories. In context of MVC, the services are structures in model layer, which deal with application logic. They manipulate and facilitate the interaction of domain object and storage abstractions.
How do I add and include user class to my MVC?
All of your classes should be added using autoloader. You should read about use of spl_autoload_register()
, preferably while using namespaces.
The initialization of instance itself should be done by a factory. That lets you decouple your code from the class name of said instance.
How do I carry user class around my application?
You don't.
PHP applications do not persists. You have an HTTP request, yo do all the things you need with it, the response is sent and application is destroyed. The instances of User
class will all be short-lived.
To recover the current user between requests you store an identifier in session. Do not dump whole objects in session. Instead, after you fetch user's identifier from session, you can recover the rest of user account details from other forms of storage (if you even need it).
This whole process should be preformed and managed by some sort of "recognition service" or "authentication service" from your model layer.
How do I perform login / logout logic and perform required actions?
The login request is at first handled by controller:
public function postLogin( $request )
{
$service = $this->serviceFactory->create('recognition');
$service->authenticate( $request->getParameter('username'),
$request->getParameter('password') );
}
The service tries to verify the user's credentials, which alters the state of model layer. The view instance then looks up that state and either redirects you to the landing page as authenticated user, or redirects you back to login page with an error message.
The service themselves would be shared between model controller and view via the factory. Which means that they would initialize each service only once and then just reuse it. Something along the lines of:
class ServiceFactory
{
private $cache = array();
public function create( $name )
{
if ( array_key_exists($name, $this->cache) === false )
{
$this->cache[$name] = new $name;
}
return $this->cache[$name];
}
}
Just keep in mind that his is an extremely simplified example.
For further reading I would recommend you to go through this collection of links. Also, you might find these 3 posts somewhat useful: this, this and this.