dounai6626 2014-05-11 03:05
浏览 49
已采纳

在Route :: model中找不到laravel 4句柄

when use "Route::model" in routes.php

Route::model('pizza', 'Pizza');

Route::get('edit/{pizza}', 'PizzaController@edit');

I receive error if "id" not found in DB, how to display custom message in this status?

thanks,

  • 写回答

1条回答 默认 最新

  • donglaoping9702 2014-05-11 03:14
    关注

    By default, Laravel will throw a 404 error if the model binding fails. So what you'll want to do is provide a closure as a third parameter to override the behavior and throw a different error:

    Route::model('pizza', 'Pizza', function()
    {
        throw new ModelNotFoundException; // or NotFoundHttpException or PizzaNotFoundException...
    });
    

    See: http://laravel.com/docs/routing#route-model-binding

    Once you have that in place, you can catch the exception and handle it however you'd like in app/start/global.php

    See: http://laravel.com/docs/errors#handling-errors

    Or instead of throwing an exception, you could just use the closure to return a Redirect response to a specific page with a flash message indicating the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动