Skip to content

Instantly share code, notes, and snippets.

@thiagorb
Last active July 29, 2024 08:44
Show Gist options
  • Save thiagorb/d4f4afaafa23a7a564b5675db952fbb2 to your computer and use it in GitHub Desktop.
Save thiagorb/d4f4afaafa23a7a564b5675db952fbb2 to your computer and use it in GitHub Desktop.
Extend session store in Laravel
<?php
namespace App\Session;
class EncryptedStore extends \Illuminate\Session\EncryptedStore
{
public function get($key, $default = null)
{
$originalValue = parent::get($key, $default);
if (is_null($originalValue)) {
// do something
}
return $originalValue;
}
}
<?php
namespace App\Session;
class SessionManager extends \Illuminate\Session\SessionManager
{
protected function buildSession($handler)
{
if ($this->app['config']['session.encrypt']) {
return $this->buildEncryptedSession($handler);
}
return new Store($this->app['config']['session.cookie'], $handler);
}
protected function buildEncryptedSession($handler)
{
return new EncryptedStore(
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
);
}
}
<?php
namespace App\Session;
class SessionServiceProvider extends \Illuminate\Session\SessionServiceProvider
{
protected function registerSessionManager()
{
$this->app->singleton('session', function ($app) {
return new SessionManager($app);
});
}
}
<?php
namespace App\Session;
class Store extends \Illuminate\Session\Store
{
public function get($key, $default = null)
{
$originalValue = parent::get($key, $default);
if (is_null($originalValue)) {
// do something
}
return $originalValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment