2 Methods to Get Session ID in Laravel 9

2 Methods to Get Session ID in Laravel 9

When working with Laravel application, there might be required to get the ID for the current session. This tutorial provides 2 methods how to get session ID in Laravel 9 application.

Method 1 - 'Session' facade

The Session facade has the getId method that allows to get session ID.

app/Http/Controllers/TestController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Response; use Illuminate\Support\Facades\Session; class TestController extends Controller { public function index(): Response { $sessionId = Session::getId(); return new Response($sessionId); } }

Method 2 - 'session' helper function

The session helper function can be used to get an instance of the Store class. It provides the getId method for retrieving the session ID.

app/Http/Controllers/TestController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Response; class TestController extends Controller { public function index(): Response { $sessionId = session()->getId(); return new Response($sessionId); } }

Leave a Comment

Cancel reply

Your email address will not be published.