laravel - 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 -IndexesRewriteEngine 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 中注册了中间件,但我仍然面临这个问题。
解决方案
推荐阅读
- c# - 正则表达式匹配字母和数字,其中一些符号可以出现在它们之间
- generics - 使用具有生命周期的参数包装函数时,类型不够通用错误
- mysql - 你可以从shell加载xml文件吗?
- swift - 重命名磁盘上的文件的权限错误
- bash - 来自“学习 Bash Shell,第 3 版”的示例练习问题。[纽汉,卡梅伦]
- tailwind-css - 如何在顺风中相对于另一个 div 设置绝对位置?
- python - 在完成所有任务之前关闭 ProcessPoolExecutor 的异步
- python - 从 Pandas 列中的字符串中选择时间段的两个单词并转换为天数
- wcsession - WCSession 检测手表关机。HKHealthStore startWatchApp 挂起
- javascript - 从javascript将mathjax方程插入innerHTML