【发布时间】:2013-09-27 08:51:42
【问题描述】:
我正在尝试在我的 Laravel 4 REST API 中创建一个默认路由,当我定义的其他路由均不匹配请求时,它会向调用者返回特定错误。
这可能吗?不幸的是,我在文档中一无所获,因此我尝试使用通配符 (*) 作为我的 routes.php 中的最后一个 Route 定义,但这不起作用。
Route::when("*", function(){
throw new CustomizedException('Route not found...');
});
当我有这条路线并执行artisan routes 时,我得到一个异常:
{"error":{"type":"ErrorException","message":"Object of class Closure could not be converted to string","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Console\/RoutesCommand.php","line":153}}
调用不存在的路由不会触发我自定义的异常,而是标准的异常:
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1429}}
我也尝试按照in this post 的建议使用any,但这也不起作用:
Route::any( '(.*)', function( $page ){
throw new ValidationException('Custom error');
});
当我调用不存在的路由时,该路由也不会触发。
任何提示,我做错了什么将不胜感激。
【问题讨论】: