Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it’s light-weight. Session might be configured within the file saved at config/session.php.

Accessing Session Data

To access the session data, we’d like an occasion of session which might be accessed by way of HTTP request. After getting the occasion, we will use the get() method, which is able to take one argument, “key”, to get the session data.

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

You can use all() method to get all session data instead of get() method.

Storing Session Data

Data might be saved in session using the put() method. The put() method will take two arguments, the “key” and the “value”.

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

Deleting Session Data

The forget() method is used to delete an item from the session. This method will take “key” as the argument.

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

Use flush() method instead of forget() method to delete all session data. Use the pull() method to retrieve information from session and delete it afterwards. The pull() technique may also take key because the argument. The distinction between the forget() and the pull() technique is that forget() technique won’t return the value of the session and pull() technique will return it and delete that value from session.

Example

Step 1 − Create a controller called SessionController by executing the following command.

php artisan make:controller SessionController

Step 2 – Add the following code in a file at app/Http/Controllers/SessionController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SessionController extends Controller {
   public function accessSessionData(Request $request) {
      if($request->session()->has('my_name'))
         echo $request->session()->get('my_name');
      else
         echo 'No data in the session';
   }
   public function storeSessionData(Request $request) {
      $request->session()->put('my_name','Virat Gandhi');
      echo "Data has been added to session";
   }
   public function deleteSessionData(Request $request) {
      $request->session()->forget('my_name');
      echo "Data has been removed from session.";
   }
}

Step 3 – Add the following lines at routes/web.php file.

Route::get('session/get','SessionController@accessSessionData');
Route::get('session/set','SessionController@storeSessionData');
Route::get('session/remove','SessionController@deleteSessionData');

Step 4 – Visit the following URL to set data in session.

http://localhost:8000/session/set

Step 5 − Visit the following URL to get data from session.

http://localhost:8000/session/get

Step 6 − Visit the following URL to remove session data.

http://localhost:8000/session/remove

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com