dongye8110 2014-07-27 23: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 06: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部