php Laravel 5 / Lumen 请求头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29906673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:39:24  来源:igfitidea点击:

Laravel 5 / Lumen Request Header?

phprestlaravellumen

提问by kevingilbert100

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 PHP variable.

所以我不太确定如何解决这个问题我已经尝试了一些事情,我将在下面列出一个但是我想要做的是将 http 请求中发送的信息存储在 PHP 变量中。

Here is a view from Chrome Postman of me sending the request I want ot send. Note "pubapi" is a "header".

这是 Chrome Postman 对我发送我想要发送的请求的看法。注意“pubapi”是一个“标题”。

PostMan View

邮递员视图

I have been messing around with Lumen requests as you can see documented here ( http://lumen.laravel.com/docs/requests) and have tried using the following below to possibly display them but its not working obviously.

正如您在此处(http://lumen.laravel.com/docs/requests)所看到的,我一直在处理 Lumen 请求,并尝试使用以下内容来显示它们,但显然无法正常工作。

echo Request::all();

I am putting this in my controller and I have ...

我把它放在我的控制器中,我有......

use Illuminate\Http\Request;

in my controller.

在我的控制器中。

So how could I say store the header I am sending "pubapi" into a php variable in my controller?

那么我怎么能说将我发送“pubapi”的标题存储到我的控制器中的php变量中呢?

EDIT

编辑

Not sure if this will help, however looking at the Laravel frameworks docs I see this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_headertrying this throws the same error in my code. So for example I tried the following and reached the same error.

不确定这是否会有所帮助,但是查看 Laravel 框架文档我看到这个http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_header尝试这会在我的代码中引发相同的错误。例如,我尝试了以下操作并遇到了相同的错误。

echo Request::header('pubapi');

采纳答案by balintant

Try to change the Illuminate\Http\Requestto Request.

尝试将 更改Illuminate\Http\RequestRequest

- use Illuminate\Http\Request;
+ use Request;

回答by Qevo

You misunderstand the Laravel request object on two levels.

你在两个层面上误解了 Laravel 请求对象。

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.

首先,您得到的错误是因为您引用的是对象而不是 Facade。Facades 有一种将静态方法调用转发到非静态方法的方法。

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).

发布到此路线,您将看到您的标题,其中之一将是pubapi。请注意,路由方法定义与您提交请求的方式(即 GET 或 POST)相匹配。

Let's apply this to the controller, ArticleController:

让我们将其应用到控制器 ArticleController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $pubapi = $request->header('pubapi'); // string
        $headers = $request->headers->all(); // array
        /*
          $pubapi === $headers['pubapi']
        */
    }
}

回答by kevingilbert100

Using

使用

echo app('request')->header('pubapi');

Instead of

代替

echo Request::header('pubapi');

Seemed to work perfect. Could someone provide additional explanation to why this worked and my original method didn't?

似乎工作完美。有人可以提供额外的解释为什么这有效而我的原始方法没有?

回答by Shahrukh Anwar

Actually you are calling it statically, that's why it is not getting appropriate Request class and throwing error, can do as follows

实际上你是静态调用它,这就是为什么它没有得到合适的请求类并抛出错误,可以做如下

use Illuminate\Http\Request;

//inside your controller
class YourClass extends Controller{
   public function yourFunction(Request $request){
        //for getting all the request
        dd($request->all());

        //for getting header content
        dd($request->header('pubapi'));
   }
}