Quick Contact

    Laravel – Sessions

    Here learn about http session. All http driven application are stateless. Session provide a way to store information about the user across multiple requests. Laravel provides various drivers like file, cookies, array and database to handle session data. By default file driver is used because it is lightweight session can be configured using its configuration file.

    So, let’s see the session configuration file.

    – Open the visual studio and go inside the config directory and in config folder open “session.php” file. Let’s see how you can use a session in Laravel 8.

    – For this create new controller. So switch to command prompt and run the command as-:

    Php artisan make: controller SessionController

    And then press enter and controller created successfully.

    – Now run the application so write-

    Php artisan serve

    Now go to project i.e. visual studio and open app> Http> Controllers> SessionController.php file. Inside file create the controller and then inside it create a function for access the system data.

    
    < ? php
    Namespace App\Http\Controllers;
    Use Illuminate\Http\Request;
    Class SessionController extends Controller
    {
      Public function getSessionData(Request $request)
      {
    }
    

    Put if condition in function. Inside if condition just gives session value.

    
    < ? php Namespace App\Http\Controllers; Use Illuminate\Http\Request; Class SessionController extends Controller { Public function getSessionData(Request $request) { If ($request->session () ->has (‘name’))
        {
        Echo $request->session () ->get(’name’)
        }
    }
    

    Let’s get session method to retrieve data from session by its key name. If session case is not present then just return a message. So write further else () with echo ()-:

    
    < ? php Namespace App\Http\Controllers; Use Illuminate\Http\Request; Class SessionController extends Controller { Public function getSessionData(Request $request) { If ($request->session () ->has (‘name’))
       {
       Echo $request->session () ->get(’name’)
        }
    Else
    {
       Echo “No data in the session”;
    }
    }
    

    Now save the file. Let’s create another function here for storing session data so write-:

    Public function storeSessionData () (function name) (Request $request) and inside this function add the value and echo () as-:

    
    {
    $request-> session ()->put(‘name’, ‘Jennifer’);
    Echo “Data has been added to the session”;
    }
    

    Now create another function for deleting Json data.

    
    Public function deleteSessionData(Request $request)
    {
        $request->session () ->forget (‘name’);
    Echo “Data has been removed from the session”;
    }
    

    In session ‘forget’ method is called and the name is passed in forget. Here forget method will remove the session by the keyname.

    The code is as-:

    
    < ? php Namespace App\Http\Controllers; Use Illuminate\Http\Request; Class SessionController extends Controller { Public function getSessionData(Request $request) { If ($request->session () ->has (‘name’))
        {
        Echo $request->session () ->get(’name’)
        }
    Else
    {
       Echo “No data in the session”;
    }
    }
       Public function storeSessionData () (Request $request)
    {
    $request-> session ()->put(‘name’, ‘Jennifer’);
    Echo “Data has been added to the session”;
    }
    Public function deleteSessionData(Request $request)
    {
        $request->session () ->forget (‘name’);
    Echo “Data has been removed from the session”;
    }
    

    Now save the file and create route for these methods. So for this go to routes directory> “web.php file”. Open web.php file and create the route in which you should also add class and controller name as-:

    Route::get(‘/session/get’,[SessionController::class, ‘getSessionData’])->name(‘session.get’);

    And also use app controller as-:

    Use App\Http\Controllers\SessionController;

    Now create another route. Apply set method here.

    Route::get(‘/session/set’, [SessionController::class, ‘storeSessionData’])->name (‘session.store’);

    One more route for delete as-:

    Route::get(‘/session/remove’, [SessionController::class, ‘deleteSessionData’])->name(‘session.delete’);

    Save the file and now check the output.

    Go to browser and > localhost:8000/session/set, press enter and see the message data.

    Now get the session data by search> localhost:8000/session/get

    Now here you can see the session data.

    Now let’s remove the session data and call the remove in url by replacing get as-:

    Localhost:8000/session/remove

    And you can see the all outputs so well with session coding.

     

     

    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.