在Route :: model中找不到laravel 4句柄 [英] laravel 4 handle not found in Route::model
问题描述
在routes.php中使用"Route :: model"时
when use "Route::model" in routes.php
Route::model('pizza', 'Pizza');
Route::get('edit/{pizza}', 'PizzaController@edit');
如果在数据库中找不到"id",我会收到错误消息,如何在此状态下显示自定义消息?
I receive error if "id" not found in DB, how to display custom message in this status?
谢谢
推荐答案
默认情况下,如果模型绑定失败,Laravel将抛出404错误.因此,您要做的是提供一个闭包作为第三个参数,以覆盖行为并引发其他错误:
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...
});
请参阅: http://laravel.com/docs/routing#route-model-绑定
将其放置到位后,就可以在app/start/global.php
Once you have that in place, you can catch the exception and handle it however you'd like in app/start/global.php
请参阅: http://laravel.com/docs/errors#handling-errors
或者不引发异常,您可以使用闭包对特定页面返回Redirect
响应,并带有指示错误的闪现消息.
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.
这篇关于在Route :: model中找不到laravel 4句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!