Session Tutorial in Laravel 11

 <?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index()
    {
        $value = session("name");
        return view("welcomeTwo", compact("value"));


        // $value = session()->all();
        // $value = session()->except(["class","_previous"]);  //Here session will be show except "class" and "_previous" session
        // $value = session()->only(["name","_previous"]); //Here session will show "name" and "_previous" session only
        // echo "<pre>";
        // print_r($value);
        // echo "</pre>";


        // If we want to check whether any particular session key exists or not as shown below :
        // we can use exits() method instead of "has()" method.
        // Difference between exits() and has() method is that if there is session key is present but its
        // value is NULL then exits() method returns TRUE and has() method returns FALSE
        // if(session()->exists("name")) {
        //     $value = session()->get("name");
        //     echo $value;
        // } else {
        //     echo "Name key doesn't Exists";
        // }

        // $value = session()->get("name");    //Read session value using "get()" method
        // $value = session("name");   //Another way for read session value without "get()" method
        // $value = session("name", "HELLO");   //we can pass default value of session as second parameter(Here "HELLO" is default value of "name"),
        // if session is not created of named "name" then default value "HELLO" will be show
        // echo $value;
    }

    public function storeSession(Request $request)
    {
        // session(["name" => "HelloWorld"]);      //creating session value with key and value
        // session()->put("class","BTech");    //creating session value with "put()" method

        // Normally "Request" class is used to accept form data but we can use "Request" class with session
        // we can also use "Request" class for store session value and delete session value
        // When we use "Request" class at that time we have to use "put()" method, we can not save data in array format as shown below :

        // session(["name" => "HelloWorld"]);  //First way for store session without "Request" class
        // $request->session()->put("class", "BTech");     //Second way for store session with "Request" class

        // we can store multiple sessions simultaneously as shown below :
        session([
            "name" => "HelloWorld",
            // "name" => NULL,
            "class" => "BTech"
        ]);

        // Below flash() method will also create a session but that session is for temporary and will remove after some seconds
        session()->flash("status", "Session saved Successfully.");

        // session()->increment("count");
        // If we want to increment by 2 then we can do as shown below :
        // session()->increment("count", $incrementBy = 2);
        // session()->decrement("count", $decrementBy = 2);

        // By default, token value is not changed, but sometimes hackers can change or retrieve our session value
        // Hence, to prevent them laravel provides a method "regenerate()", "regenerate()" method changes token
        // value every time whenever we passed "regenerate()" in any method and whenever we go in any page every time
        // token value will change
        session()->regenerate();

        return redirect("/welcomeTwo");
    }

    public function deleteSession()
    {
        // session()->forget(["name", "class"]);

        // session()->flush(); //flush() method will remove all sessions

        // invalidate() method works same as "regenerate()" method but invalidate() method will remove all sessions also
        // we can use invalidate() method at the time of logout
        session()->invalidate();
        return redirect("/welcomeTwo");
    }
}
Above File is app\Http\Controllers\TestController File




Below File is resources\views\welcomeTwo File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>welcomeTwo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
    <h1>{{ $value }}</h1>

    @if(session("status"))
        <div class="alert alert-success">
            {{ session("status") }}
        </div>
    @endif
</body>
</html>




Below File is routes\web File
<?php

use App\Http\Controllers\TestController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::controller(TestController::class)->group(function () {
    Route::get("/welcomeTwo", "index");
    Route::get("/store-session", "storeSession");
    Route::get("/delete-session", "deleteSession");
});





Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11