Laravel 5 / Lumen Request Header?

前端 未结 5 621
你的背包
你的背包 2021-02-05 04:32

So I am not really sure how to go about this I have tried a few things and I will list one below however what I am trying to do is store information sent in a http request in a

5条回答
  •  深忆病人
    2021-02-05 04:50

    You misunderstand the Laravel request object on two levels.

    First, the error you are getting is because you were referencing the object instead of the Facade. Facades have a way of forwarding static method calls to non-static methods.

    Second, you are sending the value as a header but are trying to access the request parameters. This will never give you what you want.

    Here is a simple way to see an example of what you want by creating a test route like so:

    Route::match(['get','post'], '/test', function (Illuminate\Http\Request $request) {
        dd($request->headers->all());
    });
    

    Post to this route and you will see your headers, one of which will be pubapi. Pay attention that the route method definition matches how you are submitting the request (ie GET or POST).

    Let's apply this to the controller, ArticleController:

    header('pubapi'); // string
            $headers = $request->headers->all(); // array
            /*
              $pubapi === $headers['pubapi']
            */
        }
    }
    

提交回复
热议问题