编程语言
首页 > 编程语言> > php-无法在laravel 5.6控制器中创建Request对象

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

作者:互联网

控制器名称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”时,它将显示会话值,但是我想从viewSession_v2()中删除Request $request,所以我执行了以下代码:

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

现在,当我调用“ viewSession-2”时,它将出现以下错误
Error Snap 1
Error Snap 2

请任何人告诉我如何在方法/函数中创建请求对象?为什么在这里给我一个错误?

解决方法:

您始终可以访问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);
}

标签:laravel-5-6,laravel-5,php
来源: https://codeday.me/bug/20191108/2007774.html