首页 > 解决方案 > HTTP 405 方法不允许流明/Laravel

问题描述

我试图使用 lumen/laravel 为我的反应构建一个身份验证 api,每次我想用邮递员对其进行测试时,我都会收到以下错误 HTTP 405 Method Not Allowed 。

我的路线:

$router->group(['prefix' => 'patient'], function () use ($router) {
   
    $router->post('register',[PatientAuthController::class,'register'] );
     $router->post('login', [PatientAuthController::class,'login'] );
 });

CorsMiddleware:

class CorsMiddleWare
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //Intercepts OPTIONS requests
        if ($request->isMethod('OPTIONS')) {
            $response = response('', 200);
        } else {
            // Pass the request to the next middleware
            $response = $next($request);
        }

        // Adds headers to the response
        $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE');
        $response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Expose-Headers', 'Location');

        // Sends it
        return $response;
    }

患者身份验证控制器:

    protected $patient;
    protected $utilityService;


    public function __construct()
    {
        $this->middleware("auth:patient",['except'=>['login','register']]);
        $this->patient = new Patient;
        $this->utilityService = new UtilityService;
    }


    public function register(PatientRegisterRequest $request)
   {
       $password_hash = $this->utilityService->hash_password($request->password);
       $this->patient->createPatient($request,$password_hash);
       $success_message = "registration completed successfully";
    return  $this->utilityService->is200Response($success_message);
   }


    /**
     * Get a JWT via given credentials.
     *
     * @param  Request  $request
     * @return Response
     */
    public function login(LoginRequest $request)
    {
        $credentials = $request->only(['email', 'password']);

        if (! $token = Auth::guard('patient')->attempt($credentials)) {
            $responseMessage = "invalid username or password";
            return $this->utilityService->is422Response($responseMessage);
         }
         

        return $this->respondWithToken($token);
    }

.htaccess:

选项 -MultiViews -Indexes
RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

我已经在 app.php 中注册了中间件,但我仍然面临这个问题。

标签: laravelauthenticationlumen

解决方案


推荐阅读