【问题标题】:Check which controller and method is sending request on exception handling in Laravel检查哪个控制器和方法正在发送 Laravel 异常处理请求
【发布时间】:2023-01-18 22:21:07
【问题描述】:

我想在Handler中处理异常时获取控制器和方法。

我不想在控制器上使用 try-catch。

有没有办法做到这一点?我无法在文档中找到示例。

$this->renderable(function (NotFoundHttpException $e, $request) {
            if ($request->is('api/*')) {
                //check here the controller and the method
                return responder()->error($e->getMessage())->respond(404);
            }
        });

【问题讨论】:

  • 不是进入 Laravel,只是为了澄清:如果此时你有一个 NotFound Exception,当当前路由似乎不存在时,你正在寻找女巫控制器和方法?
  • @Foobar NotFoundHttpException 在找不到模型时抛出。路由存在,它来自试图找到模型数据的控制器和方法
  • 好的,谢谢你的回复。

标签: php laravel error-handling


【解决方案1】:

您可以使用这些路由方法:

$request->route()->getControllerClass();

$request->route()->getActionMethod();

您可以在此处查看这些和其他路由方法:https://laravel.com/api/9.x/Illuminate/Routing/Route.html

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2015-10-22
    • 2015-03-30
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多