laravel 4 handle not found in Route::model

假如想象 提交于 2019-12-23 05:47:04

问题


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:


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.



来源:https://stackoverflow.com/questions/23588480/laravel-4-handle-not-found-in-routemodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!