Reputation: 11
I want to create a custom (database-) session driver in Laravel 4. The existing Laravel database driver only allows for a database table in a static format with one data-field, while I have an existing database table with multiple fields and formats.
I expected to be able to somehow use the Session::extend function, the same way as could be done with Auth::extend, but this does not seem to be the case. It also seems that the session is already created before the global.php file is even run.
The only working solution I have got so far is to copy paste most of the involved files (PdoSessionHandler, SessionManager and SessionServiceProvider) and sort of duck-tape it together. This is obviously a solution that will create problems in the future.
How do I create my own custom session driver?
Upvotes: 1
Views: 1268
Reputation: 515
You are correct that you need to extend the SessionHandler and create a new ServiceProvider, then set the config to point the session to your new provider. Your should be able to do your special magic with the new database fields in your new Handler.
class CompanySessionHandler extends DatabaseSessionHandler {
/**
* {@inheritDoc}
*/
public function read($sessionId)
{
//read from your database
}
/**
* {@inheritDoc}
*/
public function write($sessionId, $data)
{
//write to your database
}
}
Im not sure what else you are looking for besides that.
Upvotes: 1