Return 在 laravel 中使用了错误的 http 请求类型(例如 get 而不是 post)时出现 404
Return a 404 when wrong ttp request type used(e.g. get instead of post) in laravel
我正在开发 rest API,有时前端会调用带有错误 HTTP 请求类型的端点。比如我有一条路线(/users/unassigned
),路线的类型是"GET"。想象一下我的前端使用 "POST" 请求调用这条路由。出现以下错误。
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, DELETE.
我想要的是 API 在这些情况下响应 JSON 我可以处理异常。
我使用了 Route::fallback
但是这个方法捕捉路线的每一个例外。我需要一个只处理上述问题的函数。
检查您在 $request 变量中收到的请求类型,然后使用条件处理它。
一种方法是用app/Exceptions/Handler.php
来处理它:
public function render($request, Exception $e)
{
if ($request->ajax() || $request->wantsJson()) {
return response()->json([
'error' => $e->getMessage(),
], 400);
}
return parent::render($request, $e);
}
当请求由 ajax 或请求期望 json.
发出时,这将针对所有异常输出错误消息 json
您还可以像这样添加异常类型检查:
if ($exception instanceof MethodNotAllowedHttpException) {
// Code...
}
您可以在异常处理程序中进行一些调整。
在 app/Exceptions/Handler.php
文件的渲染函数中
public function render($request, Exception $exception)
{
if ($exception instanceof MethodNotAllowedHttpException) {
if ($request->ajax()) {
return response()->json(['error' => 'Route Not Found'], 404);
}
else {
//something else
}
}
return parent::render($request, $exception);
}
并在顶部使用部分添加 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
。
非常感谢。
我在 handler.php.
中使用了以下代码
{
if ($this->isHttpException($exception)) {
switch ($exception->getStatusCode()) {
// not authorized
case '403':
return Response()->json(["error" => "not authorized."],403);
break;
// not found
case '404':
return Response()->json(["error" => "Route not found."],404);
break;
// internal error
case '500':
return Response()->json(["error" => "internal error."],500);
break;
case '405':
return Response()->json(["error" => "request type not match."],405);
break;
default:
return $this->renderHttpException($exception);
break;
}
}
else {
return parent::render($request, $exception);
}
}
要点是 http 代码 405 表示不允许的方法。特别感谢@apokryfos。
改变你的Handler.php
喜欢
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
//
];
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
$arr = array("status" => 400, "message" =>"Unauthorized access", "data" => array());
return \Response::json($arr);
}
return redirect()->guest('login');
}
我正在开发 rest API,有时前端会调用带有错误 HTTP 请求类型的端点。比如我有一条路线(/users/unassigned
),路线的类型是"GET"。想象一下我的前端使用 "POST" 请求调用这条路由。出现以下错误。
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, DELETE.
我想要的是 API 在这些情况下响应 JSON 我可以处理异常。
我使用了 Route::fallback
但是这个方法捕捉路线的每一个例外。我需要一个只处理上述问题的函数。
检查您在 $request 变量中收到的请求类型,然后使用条件处理它。
一种方法是用app/Exceptions/Handler.php
来处理它:
public function render($request, Exception $e)
{
if ($request->ajax() || $request->wantsJson()) {
return response()->json([
'error' => $e->getMessage(),
], 400);
}
return parent::render($request, $e);
}
当请求由 ajax 或请求期望 json.
发出时,这将针对所有异常输出错误消息 json您还可以像这样添加异常类型检查:
if ($exception instanceof MethodNotAllowedHttpException) {
// Code...
}
您可以在异常处理程序中进行一些调整。
在 app/Exceptions/Handler.php
文件的渲染函数中
public function render($request, Exception $exception)
{
if ($exception instanceof MethodNotAllowedHttpException) {
if ($request->ajax()) {
return response()->json(['error' => 'Route Not Found'], 404);
}
else {
//something else
}
}
return parent::render($request, $exception);
}
并在顶部使用部分添加 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
。
非常感谢。
我在 handler.php.
{
if ($this->isHttpException($exception)) {
switch ($exception->getStatusCode()) {
// not authorized
case '403':
return Response()->json(["error" => "not authorized."],403);
break;
// not found
case '404':
return Response()->json(["error" => "Route not found."],404);
break;
// internal error
case '500':
return Response()->json(["error" => "internal error."],500);
break;
case '405':
return Response()->json(["error" => "request type not match."],405);
break;
default:
return $this->renderHttpException($exception);
break;
}
}
else {
return parent::render($request, $exception);
}
}
要点是 http 代码 405 表示不允许的方法。特别感谢@apokryfos。
改变你的Handler.php
喜欢
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
//
];
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
$arr = array("status" => 400, "message" =>"Unauthorized access", "data" => array());
return \Response::json($arr);
}
return redirect()->guest('login');
}