Thứ Hai, 5 tháng 9, 2016

Laravel 5 Tutorial : Get() Put() Remove() Method HTTP Session in laravel 5.3


Laravel 5 Tutorial - How we can use Get() method in HTTP Session, Put() method and Remove() Method HTTP Session in laravel 5.3? in the previews lessons, we have learn about Create localization for Multiple languages and Authentication Login & Registration Form + Bootstrap

Get() method in HTTP Session

Get() method in HTTP Session is used for Accessing Session Data, we need an instance of session which can be accessed via HTTP request.

Put() method in HTTP Session

Put() can use for Storing Session Data, The put() method will take with two arguments, the "key" and the "value".

Forget() method in HTTP Session

forget() method use for Deleting Session Data, more detail about Http Session following Laravel Document https://laravel.com/docs/5.3/session#storing-data

Example Laravel Project (Http Session)

At your laravel project, we need to create new Controller, create new controller using Artisan command following by :

php artisan make:controller SessionController

Your SessionController.php will created and stored on App\Http\Controllers
Just create all function for Get() Put() Remove() Method int your controller,
Get() Put() Remove() Method HTTP Session in laravel 5.3

SessionController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SessionController extends Controller
{
    //we will create function to get accessing session Data
    // that is with get() method
    public function getSession(Request $request){
      if($request->session()->has('session_name'))
        echo $request->session()->get('session_name');
      else {
        echo 'no data in the session';
      }
    }
    // create new function for put method
    public function putSession(Request $request){
      $request->session()->put('session_name','www.hc-kr.com');
      echo 'a data hasbeen added to the session';
    }
    // create new function to delete session
    public function forgetSession(Request $request){
      $request->session()->forget('session_name');
      echo "data hasbeen removed from the session";
    }
}


After all done, just create new routes in your route file (\routes\web.php for Laravel 5.3)

web.php

Route::get('session/get', 'SessionController@getSession');
Route::get('session/put', 'SessionController@putSession');
Route::get('session/forget', 'SessionController@forgetSession');

Try to access your route in your browser following this command

http://localhost:8080/session/get
http://localhost:8080/session/put
http://localhost:8080/session/forget

Video Tutorial Get() Put() Remove() Method HTTP Session


See you next Lessons ....

Không có nhận xét nào:

Đăng nhận xét