Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel REST API请求对象为空 环境_Php_Laravel_Ubuntu_Nginx - Fatal编程技术网

Php Laravel REST API请求对象为空 环境

Php Laravel REST API请求对象为空 环境,php,laravel,ubuntu,nginx,Php,Laravel,Ubuntu,Nginx,我使用Laravel5.7创建了一个应用程序,并实现了一个RESTAPI。我在routes/api.php中有一个路由,它触发一个中间件,该中间件检查传入请求是否有一个名为api_token的参数 这是一个生产环境,具体内容如下: Route::middleware('rest')->group(function() { Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocatio

我使用Laravel5.7创建了一个应用程序,并实现了一个RESTAPI。我在routes/api.php中有一个路由,它触发一个中间件,该中间件检查传入请求是否有一个名为api_token的参数

这是一个生产环境,具体内容如下:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
  • Linux Ubuntu 18.04 LTS 64位
  • nginx/1.14.0
  • 拉威尔5.7
  • PHP7.2
.ENV文件中的APP_ENV设置为“production”,APP_DEBUG设置为“false”

问题 我的问题是,传入的请求对象到达服务器时总是空的。至少我的Laravel应用程序是这么说的

这些是我在routes/api.php中的路由:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
正如您所看到的,这些路由位于一个名为“rest”的中间件组中。我正在使用路由::get('device-location/{deviceID}','PositionDataController@getDeviceLocation'); 测试功能的路径

以下是来自中间件的代码:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
中间件首先检查请求对象中是否有参数,然后执行一些逻辑来检查是否发送了正确的令牌。如果请求对象为空,它将返回一些数据,以帮助我了解出了什么问题

我使用Postman()来测试API。这是我的邮递员设置:

邮差设置

邮递员标题

这是我在《邮差》中得到的回应:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
邮差回复

如果在浏览器中调用该路由,则会得到相同的结果。 不管我是否输入参数,请求似乎总是空的

以下是我尝试做的事情:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
  • 不使用中间件
  • 使用$request变量而不是helper request()
  • 在我的邮递员设置的标题中的“application/json”和“application/x-www-form-urlencoded”之间切换
  • 在浏览器中调用该路由
  • 更新至Laravel 5.7
奇怪的是,所有这些在我的本地环境和与生产服务器规格相同的测试服务器上都能完美工作

更新01:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});
public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}
所以看起来更糟

如果我在web.php中添加这样的路由:

Route::get('/request-return', function() {
    return request()->all();
});
然后像这样访问该路线:

laravel.application/request-return?device_id=1&api_token=XgkQLs8G7OYTqdwsXmjJT5G9bxv20DRi
我得到一个空数组[]


因此,参数似乎无法到达服务器本身。

您通过get请求获取设备id,因此请使用下面的行,而不是
$request()->device\u id

用这个让我知道
$name=$request->input('device_id')

好的,我可以解决这个问题。这与拉威尔无关。这是一个nginx配置问题:


所以如果我添加
$name=$request->input('device_id')
返回['name'=>$name]