3

Controller name MyController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request){
        //
        $request->session()->put('key', '123');
        $data = $request->session()->get('key');
        print_r($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id){
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id){
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        //
    }
    public function contact(){
        return view('contact');
    }

    public function createSession(Request $request){
        //
        $request->session()->put('key', '123');
        $data = $request->session()->get('key');
        print_r($data);
    }
    public function viewSession_v1(Request $request){
        $data = $request->session()->get('key');
        print_r($data);
    }
    public function viewSession_v2(Request $request){
        $data = $request->session()->get('key');
        print_r($data);
    }
}

Route:

Route::get('createSession', 'MyController@createSession');
Route::get('viewSession-1', 'MyController@viewSession_v1');
Route::get('viewSession-2', 'MyController@viewSession_v2');

Now when I call "createSession" it will create new session and when i call either "viewSession-1" or "viewSession-2" it will display session value but I want to remove Request $request from viewSession_v2() so I did following code:

public function viewSession_v2(){
        $request = new \Illuminate\Http\Request();
        $data = $request->session()->get('key');
        print_r($data);
    }

Now when I call "viewSession-2" it will give me following error Error Snap 1 Error Snap 2

Please anyone can tell me how can I create request object in method/function? and why it gives me an error here?

3
  • 1
    Possible duplicate of Laravel - Session store not set on request Commented Nov 19, 2018 at 7:50
  • No it's not duplicate question. Commented Nov 19, 2018 at 7:51
  • Definitely not a duplicate. Commented Nov 19, 2018 at 8:11

2 Answers 2

3

You always have access to the request() and session() helpers:

public function viewSession_v2(){
      $data = request()->session()->get('key');
      print_r($data);
}

or

public function viewSession_v2(){
      $data = session()->get('key');
      print_r($data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It looks nice for me
0

As we know Laravel provides web middleware concepts. I think you should use to remove the session request.

Route::group(['middleware' => ['web']], function () {
    // your routes here
});

More Info

Or you can put this condition as:

public function viewSession_v2(Request $request){
    if($request->hasSession()) {
            $data = $request->session()->get('key');
            print_r($data);
        }
}

7 Comments

No i have to create request object here in the function. i can't do that in route.
I don't understand when i create request object as function parameter it works perfectly then why it's not working in the function body?
Is there a reason you can't just use the request() helper?
Please can you explain how i use request() helper for session?
As we know that the session() is a helper that gives you a faster access to request()->session().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.