Laravel POST请求错误405:MethodNotAllowedHttpException

时间:2017-02-02 19:11:56

标签: php laravel rest api http

在localhost上使用postman,我可以在我的API上成功使用POST。但是当部署到服务器并使用postman时,我总是得到错误405:MethodNotAllowedHttpException,即使为路由声明了POST请求,从中间件获取了我们的verifycsrf,并取出了Web中间件。还跟随barryvdh的laravel项目的CORS安装。

想知道是否有人有任何建议来解决这个问题?

内核:         

    namespace App\Http;

    use Illuminate\Foundation\Http\Kernel as HttpKernel;

    class Kernel extends HttpKernel
    {
        /**
         * The application's global HTTP middleware stack.
         *
         * These middleware are run during every request to your application.
         *
         * @var array
         */
        protected $middleware = [
            \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            'Barryvdh\Cors\HandleCors',

        ];

        /**
         * The application's route middleware groups.
         *
         * @var array
         */
        protected $middlewareGroups = [
            'web' => [
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
            ],

            'api' => [
                'throttle:60,1',
            ],

            'admin' => [
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
                \App\Http\Middleware\PermissionAdminMiddleware::class,
            ],

        ];

        /**
         * The application's route middleware.
         *
         * These middleware may be assigned to groups or used individually.
         *
         * @var array
         */
        protected $routeMiddleware = [
            'auth' => \App\Http\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        ];
    }

cors.php         

    return [
        /*
         |--------------------------------------------------------------------------
         | Laravel CORS
         |--------------------------------------------------------------------------
         |
         | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
         | to accept any value.
         |
         */
        'supportsCredentials' => false,
        'allowedOrigins' => ['*'],
        'allowedHeaders' => ['*'],
        'allowedMethods' => ['*'],
        'exposedHeaders' => [],
        'maxAge' => 5,
        'hosts' => [],
    ];

app.php包括Barryvdh \ Cors \ ServiceProvider :: class,

routes.php文件

       Route::group(

                [
                    'prefix' => 'api',
                    'middleware' => ['cors']
                ],

                function()
                {
                    Route::post('dummy', 'API\UsersController@dummy');

                }
    );

UsersController.php         

    namespace App\Http\Controllers\API;

    use App\Http\Controllers\FP\UserController;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use Illuminate\Database\Eloquent\ModelNotFoundException;

    class UsersController extends Controller
    {
        function dummy(Request $request){
          return "test";
        }
    }

1 个答案:

答案 0 :(得分:0)

将postman中的Accept标头添加到application / json。 enter image description here