Skip to content

Laravel Integration

Ayrton Fidelis edited this page Oct 16, 2018 · 1 revision

Integrating PHPSess with Laravel

Because PHPSess implements the SessionHandlerInterface, you can easily integrate it with Laravel:

<?php

namespace App\Providers;

use PHPSess\SessionHandler;
use PHPSess\Storage\FileStorage;
use PHPSess\Encryption\OpenSSLEncryption;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;

class SessionServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Session::extend('phpsess', function ($app) {
            $sessionEncryption = new OpenSSLEncryption('the-app-key');
            $sessionStorage = new FileStorage();
            
            return new SessionHandler($sessionEncryption, $sessionStorage);
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Once the session driver has been registered, you may use the phpsess driver in your config/session.php configuration file.

Example adapted from the Laravel documentation.

Clone this wiki locally