无法在laravel 5.6控制器中创建Request对象

时间:2018-11-19 07:47:19

标签: php laravel-5 laravel-5.6

控制器名称 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::get('createSession', 'MyController@createSession');
Route::get('viewSession-1', 'MyController@viewSession_v1');
Route::get('viewSession-2', 'MyController@viewSession_v2');

现在,当我致电"createSession"时,它将创建新的会话,而当我致电"viewSession-1""viewSession-2"时,它将显示会话值,但我想从{中删除Request $request {1}},所以我执行了以下代码:

viewSession_v2()

现在,当我致电public function viewSession_v2(){ $request = new \Illuminate\Http\Request(); $data = $request->session()->get('key'); print_r($data); } 时,会出现以下错误 Error Snap 1 Error Snap 2

请任何人告诉我如何在"viewSession-2"中创建请求对象,为什么在这里给我一个错误?

2 个答案:

答案 0 :(得分:3)

您始终可以访问request()session()助手:

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

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

答案 1 :(得分:0)

我们知道Laravel提供了web middleware个概念。我认为您应该使用删除会话请求。

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

More Info

或者您可以将此条件设置为:

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