Многосессионные столы в Laravel - PullRequest
0 голосов
/ 21 ноября 2018

Я настроил проект Laravel с двумя сторожами с таблицей Привет своих пользователей.Теперь я хочу сохранить сессии в базу данных, а не в файл.Проблема в том, что я не могу сохранить сеанс в одной таблице, потому что идентификатор пользователя не уникален.Как я могу решить эту проблему?Благодарю.

1 Ответ

0 голосов
/ 23 ноября 2018

Я попробовал это.Это работает, но существует драйвер сеанса по умолчанию, поэтому у пользователя есть сеанс в таблице по умолчанию и в таблице admin_sessions.

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Administrator::class => UserPolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();

        Auth::extend('admin_session', function ($app, $name, array $config) {

            $provider = Auth::createUserProvider($config['provider']);
            $guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

            if (method_exists($guard, 'setCookieJar')) {
                $guard->setCookieJar($this->app['cookie']);
            }

            if (method_exists($guard, 'setDispatcher')) {
                $guard->setDispatcher($this->app['events']);
            }

            if (method_exists($guard, 'setRequest')) {
                $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
            }

            return $guard;
    }
}


class SessionServiceProvider extends ServiceProvider
{

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
    }


    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->session->extend('admin_database', function ($app) {

            $table = 'administrator_sessions';

            // This is not workin, because I don't have the user her.
            /*if (auth()->user() instanceof Customer) {
                $table = 'customer_sessions';
            }
            else if (auth()->user() instanceof Administrator){
                $table = 'administrator_sessions';
            }*/

            $lifetime = $app['config']['session.lifetime'];
            $connection = $app['db']->connection($app['config']['session.connection']);

            return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
        });
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...