dongye8110 2014-07-28 07:31
浏览 416
已采纳

使用命名空间处理Laravel ModelNotFoundexception

I have this problem where laravel ModelNotFound exeption handler catches all exceptions not only releated to eloquent.

I have added to global.php this code:

// this is default code
App::error(function(Exception $exception, $code)
{
    Log::error($exception);
});
// added code
App::error(function(ModelNotFoundException $exception)
{
    //do something, in my case redirect to 404 or whaterver
});

Controller has this partial code:

namespace CompanyName\Admin;

class PromosController extends CompanyNameAdminController {

    public function show($id) {
        $promo = Promo::findOrFail($id);
    }
}

Now if I try to pass id that dont exist I will recieve white page, code in app error does not execute. Other problem is that this App:error catches all Exeptions, like not found conrollers etc, basicly every exeption.

What I am doing wrong? I got this idea from Laracasts Exeptions handling episode, but somehow Im missing something. Because I get no feedback form Laravel about problem I am stuck. I suspect that namespaces has something to do with it but I am not sure..

EDIT: At last I found some feedback in console: 500 Internal Server Error HTML: Reload the page to get source for: http://cms.dev/admin/promos/533

  • 写回答

2条回答 默认 最新

  • douhuan1901 2015-11-05 14:44
    关注

    ModelNotFoundException is not in that namespace, so it is not being found. Specify the qualified name:

    App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception)
    {
        //do something, in my case redirect to 404 or whaterver
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?