Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laithz's avatar
Level 1

Laravel and Yii2 sharing the same session

I've been building a SaaS app using Yii2 Framework for a while now and I want to start building all new major features/modules in Laravel. This is a phase I have to transition through because I cannot pause and rebuild everything in Laravel. I want my user to be able to jump between the Laravel parts and the Yii2 parts seamlessly without having to log in twice. I will have each part under a subdomain. Let's I'm building a new admin dashboard, and let's say my old Yii2 parts are under "yii.domain.com" and the new Laravel parts are under "laravel.domain.com". I want the user to log in on "laravel.domain.com" and be automatically logged in on "yii.wodworx.com", or vice versa. Has anyone had to do it or come across something that might help me design a solution for sharing sessions across Laravel and Yii2?

0 likes
1 reply
LaryAI's avatar
Level 58

To share sessions between Laravel and Yii2, you need to ensure that both frameworks are using the same session storage mechanism and that the session data is accessible across subdomains. Here’s a step-by-step guide to achieve this:

Step 1: Use a Shared Session Storage

Both Laravel and Yii2 support multiple session storage mechanisms like database, Redis, Memcached, etc. For this example, we'll use Redis as the shared session storage.

Laravel Configuration

  1. Install the Redis package if you haven't already:

    composer require predis/predis
    
  2. Configure Redis in config/database.php:

    'redis' => [
        'client' => env('REDIS_CLIENT', 'predis'),
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],
    ],
    
  3. Set the session driver to Redis in config/session.php:

    'driver' => env('SESSION_DRIVER', 'redis'),
    
  4. Ensure the session cookie is accessible across subdomains by setting the domain in config/session.php:

    'domain' => '.domain.com',
    

Yii2 Configuration

  1. Install the Redis extension if you haven't already:

    composer require yiisoft/yii2-redis
    
  2. Configure Redis in config/web.php:

    'components' => [
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => '127.0.0.1',
            'port' => 6379,
            'database' => 0,
        ],
        'session' => [
            'class' => 'yii\redis\Session',
            'redis' => [
                'hostname' => '127.0.0.1',
                'port' => 6379,
                'database' => 0,
            ],
            'keyPrefix' => 'session:',
            'cookieParams' => [
                'domain' => '.domain.com',
            ],
        ],
    ],
    

Step 2: Synchronize Session Data

Ensure that both Laravel and Yii2 are using the same session ID. When a user logs in on one platform, the session ID should be the same on the other platform.

Laravel Middleware

Create a middleware in Laravel to set the session ID from Yii2 if it exists:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Session;

class SyncSession
{
    public function handle($request, Closure $next)
    {
        if ($request->hasCookie('PHPSESSID')) {
            Session::setId($request->cookie('PHPSESSID'));
        }

        return $next($request);
    }
}

Register the middleware in app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // other middlewares
        \App\Http\Middleware\SyncSession::class,
    ],
];

Yii2 Behavior

In Yii2, you can create a behavior to set the session ID from Laravel if it exists:

namespace app\components;

use Yii;
use yii\base\Behavior;
use yii\web\Controller;

class SyncSessionBehavior extends Behavior
{
    public function events()
    {
        return [
            Controller::EVENT_BEFORE_ACTION => 'syncSession',
        ];
    }

    public function syncSession()
    {
        if (isset($_COOKIE['laravel_session'])) {
            Yii::$app->session->setId($_COOKIE['laravel_session']);
        }
    }
}

Attach the behavior to your controllers in config/web.php:

'components' => [
    'controllerMap' => [
        'site' => [
            'class' => 'app\controllers\SiteController',
            'as syncSession' => 'app\components\SyncSessionBehavior',
        ],
    ],
],

Step 3: Ensure Consistent Authentication

Make sure that the authentication logic is consistent between both frameworks. You might need to customize the authentication process to ensure that user data is synchronized.

By following these steps, you should be able to share sessions between Laravel and Yii2, allowing users to seamlessly navigate between the two parts of your application without having to log in twice.

Please or to participate in this conversation.