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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试