我正在发送POST请求但是Laravel选择了GET

时间:2017-03-03 14:32:07

标签: php laravel

我正在向我的API'/ api / authenticate'

提交请求
Route::post('authenticate', 'api\authenticateController@authenticate');

每次发送“POST”请求服务器都认为我正在发送“GET”请求。

在$ / public / index.php中我添加了以下行来转储我正在使用的方法:

if($_SERVER["REQUEST_URI"] == "/api/authenticate") {
        dd(Illuminate\Http\Request::capture());
}

我一直在

#method: "GET"

我跑了:

composer dumpautoload
composer update
php artisan cache:clear
php artisan route:clear

然而没有任何帮助......可能是什么问题?

P.S。在本地主机上查找工作,并且我一直在使用PostMan进行测试;以下是我使用邮递员执行提交的屏幕截图:http://prntscr.com/efjwb7

以下是$ \ public \ index.php文件的代码;你可以看到我在哪里做了dd()......

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/

require __DIR__.'/../bootstrap/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

if($_SERVER["REQUEST_URI"] == "/api/authenticate") {
    dd(Illuminate\Http\Request::capture());
}

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

快速记录 - 我在index.php文件的最顶部添加了var_dump($ _ POST),并且var_dump恰好是空的。 *它似乎只影响“api”下的路线。

更新 - 这是来自Postman的cURL调用示例

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost/api/authenticate",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"api_key\"\r\n\r\njLiHWIwfi3KwGCt7OhQtFa8AAg4Ca\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"user\"\r\n\r\npro\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\npassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

1 个答案:

答案 0 :(得分:-1)

与斜线相关的配置是什么?当我发送像“site.com/url”这样的请求并且服务器将我重定向到“site.com/url/”时,我遇到了同样的问题。所以,我认为你需要检查ngninx / apache配置。