php

The Laravel 9 Session

What Are Laravel 9 Sessions?

Laravel 9 Sessions typically work to store application information about the system used across the requests. Laravel 9 has multiple drivers like file, APC, cookie, Memcached, array, database, and Redis to handle Laravel session data. By system default, the file driver is used as a Laravel session because it is lightweight. The session was located in the file to the folder at config/session.php.

How To Get Access to Session Data Laravel 9

We create an instance session to access session data for Laravel 9. This can help us access through an HTTP request. To get access, we will use the get() method in our application which has an argument ‘key’ to get session data.

$value = $request->session()->get('key');

How To Store Session Data Laravel 9

By default, the put() method is used to store session data. It has two arguments one is ‘key,’ and another is ‘Value’.

$request-> session()->put(key, ‘value’);

How To Delete Session Data in Laravel 9

In Laravel 9, to delete or remove an item from the session, apply the forget() method. This method needs only one argument, which is session ‘key’:

$request->session()->forget(key);

This command is used when we need to delete only one key. But if we like to delete all sessions, we have to use the flush() method.

$request->session()->flush();

Let’s start with an example for this project.

  1. Step 1: Create a Session Project
  2. Step 2: Create and Configure a Controller for the Session Project
  3. Step 3: Run and Test the project

Step 1: Create a Session Project

We have to run this command to create a session project.

composer create-project laravel/laravel sessionProject

We will use Visual Studio Code as an editor for our Laravel 9 project. You can try another one.

After creating the Laravel 9 project, we need to create a controller in our project. Using this controller, we will store, get, and delete the session.

Step 2: Create and Configure a Controller for the Session Project

For creating a Session controller, we have to run this command:

php artisan make:controller SessionsController

Once the controller is created, we must create three functions for our session project.

To store, use the following function:

public function setSessionData(Request $request)
    {
        $request->session()->put('web', 'www.ronbd.com');

        dd('Data has been added to your session');
}

This function will use store session data to file.

For access, use the following function:

public function getAccessSession(Request $request)
    {
        $value =  $request->session()->get('web');

        if ($request->session()->has('web')) {
            dd($value);
        } else {
            dd('Nothing to show in this session');
        }
    }

This function will use get data from the file.

To delete, use the following function:

public function deleteSetSessionData(Request $request)
    {
        $request->session()->forget('web');
        dd('Data has been removed from your session');
    }

This function will use delete the session.

All codes look like the following codes . Go to the controller we just created. Now, you need to use “app\Http\Controllers\SessionsController.php”:

class SessionsController extends Controller
{
    public function setSessionData(Request $request)
    {
        $request->session()->put('web', 'www.ronbd.com');

        dd('Data has been added to your session');
    }
    public function getAccessSession(Request $request)
    {
        $value =  $request->session()->get('web');

        if ($request->session()->has('web')) {
            dd($value);
        } else {
            dd('Nothing to show in this session');
        }
    }
    public function deleteSetSessionData(Request $request)
    {
        $request->session()->forget('web');
        dd('Data has been removed from your session');
    }
}

You can copy the code to your controller.

Now, we need to create the route for these three functions.

For that, you have to go to the following routes/web and add those three routes:

Route::get('/setSession', [SessionController::class, 'setSessionData'])->name('setSessionData');
Route::get('/getAccessSession', [SessionController::class, 'getAccessSession'])->name('getAccessSession');
Route::get('/deleteSessionData', [SessionController::class, 'deleteSetSessionData'])->name('deleteSetSessionData');

Step 3: Run and Test the Project

To run it, we need to go to the controller we just created for this session project. We run the following command:

php artisan serve

Now, go to the following route:

http://127.0.0.1:8000/setSession

It means our session created the following:

It means our session created the following:

http://127.0.0.1:8000/getAccessSession

You will get the session here:

Now, delete the session and go to the following link:

http://127.0.0.1:8000/deleteSessionData

It will delete the session, as shown below:

Conclusion:

This article discussed the three-step process to create a Laravel 9 session project with Laravel 9. The steps include creating a session project, creating and configuring a controller for the session project, and running and testing the project. We hope you found this article helpful on how the Laravel session works.

About the author

Rakhibul Hasan

This is Rakhibul Hasan CEO and Founder of Clipping Path Creative Inc. We provide 100% handmade photo editing services to use Adobe Photoshop. Our services are Clipping path, Background removal, Color correction, Photo retouching, Car photo editing, drop shadow etc. Also we offer an image as a trial to judge our quality. So contact us and get instant reply.