错误(405)使用POST时不允许使用方法

时间:2017-09-07 20:42:42

标签: php laravel-5 routes dingo-api

我正在使用Laravel 5.4和JWTAuth& Dingo由于某种原因我现在不再能够与Postman发出POST请求了。这在我第一次设置时有效,但不是我得到了响应 405 Method Not Allowed 这似乎已经在这里提出了几次,但我似乎无法找到解决方案。我已经清除了路由缓存,当我执行 api:routes 时,正确的路由就在那里。

下面是路由文件,以及它应该发送的控制器。我只遇到了LeadController路线的问题。

api.php

use Dingo\Api\Routing\Router;

/** @var Router $api */
$api = app(Router::class);

$api->version('v1', function (Router $api) {
    $api->group(['prefix' => 'auth'], function(Router $api) {
        //$api->post('signup', 'App\\Api\\V1\\Controllers\\SignUpController@signUp');
        $api->post('login', 'App\\Api\\V1\\Controllers\\LoginController@login');

        $api->post('recovery', 'App\\Api\\V1\\Controllers\\ForgotPasswordController@sendResetEmail');
        $api->post('reset', 'App\\Api\\V1\\Controllers\\ResetPasswordController@resetPassword');
    });

    $api->group(['middleware' => 'jwt.auth'], function(Router $api) {
        $api->get('protected', function() {
            return response()->json([
                'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
            ]);
        });

        $api->get('refresh', [
            'middleware' => 'jwt.refresh',
            function() {
                return response()->json([
                    'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
                ]);
            }
        ]);

        $api->post('lead/store', 'App\\Api\\V1\\Controllers\\LeadController@store');
        $api->get('lead', 'App\\Api\\V1\\Controllers\\LeadController@index');

    });
});

LeadController.php

namespace App\Api\V1\Controllers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Lead;
use App\User;

class LeadController extends Controller
{
    use Helpers;

    public function index(Lead $leads)
    {
        $leads = $leads->all();

        $count = 0;
        foreach($leads as $key => $lead){
            $user = User::where('id', $lead->user_id)->first();
            $leads[$count]['name'] = $user->name;

            array_pull($leads[$count], 'user_id');
            $count++;
        }

        return $leads;
    }

    public function store(Request $request)
    {

        $today = new Carbon();

        $this->validate(request(), [
            'owner' => 'required',
            'bname' => 'required|min:3|max:255',
            'tname' => 'max:255',
            'created' => 'required|date|before_or_equal:today',
            'update' => 'date'
        ]);

        if(!$user = User::where('zoho_id', $request->get('owner'))->first())
            return $this->response->error('invalid_owner', 500);

        $lead = new Lead;
        $lead->user_id = $user->id;
        $lead->bname = $request->get('bname');
        $lead->tname = $request->get('tname');
        $lead->created_at = $request->get('created');
        $lead->updated_at = $request->get('updated');

        if($lead->save())
            return $this->response->created();
        else
           return $this->response->error('could_not_create_lead', 500);
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了答案,为此我几乎完成了写这个问题的答案: https://laracasts.com/discuss/channels/laravel/dingo-api-and-postman-not-matching-post-request

从请求网址中删除尾随/。所以在这里留下这个答案,希望它可能对某人有用。

即。 http://api.someurl.app/api/lead/store?token= ....

也许,有人可以提出一种允许尾随/ ??

的方法