Laravel 4:创建默认路由 [英] Laravel 4: Create a default route

查看:99
本文介绍了Laravel 4:创建默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel 4 REST API中创建一个默认路由,当其他定义的路由均不符合请求时,该默认路由会返回特定错误给调用者.

I'm trying to create a default route in my Laravel 4 REST API, that hits, when none of my other defined routes matches the request, to return a specific error to the caller.

这可能吗?不幸的是,我在文档中什么都没找到,所以我玩转并尝试使用通配符(*)作为我的routes.php中的最后一个Route定义,但这不起作用.

Is this possible? Unfortunately, I found nothing in the docs, so I played around and tried using a wildcard (*) as the last Route definition in my routes.php but that doesn't work.

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}}

When I have this route and do an artisan routes, I get an Exception: {"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}}

Calling an inexistent route does not trigger my customized Exception, but the standard one: {"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1429}}

我还尝试按照建议在这篇文章中使用any ,但这也不起作用:

I also tried using any as suggested in this post, but that also doesn't work:

Route::any( '(.*)', function( $page ){
    throw new ValidationException('Custom error');
});

当我呼叫一条不存在的路由时,该路由也不会触发.

This route also doesn't fire, when I call an inexistent route.

任何提示,我做错了都会受到赞赏.

Any hints, what I'm doing wrong would be appreciated.

推荐答案

如果没有匹配的路由,Laravel会抛出"Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException".您只需编写自己的错误处理程序即可捕获该异常并执行其他操作(请查看 http://laravel.com/docs/errors ).

If there is no matching route, Laravel throws a "Symfony\Component\HttpKernel\Exception\NotFoundHttpException". You can simply write your own error handler to catch that exception and do something else (have a look at http://laravel.com/docs/errors).

添加以下两个块之一(例如,在应用程序错误处理程序"块的"app/start/global.php"中):

Add one of the following two blocks (e.g. in your "app/start/global.php" in the "Application Error Handler" block):

App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
    // do something
});

或者:

App::missing(function($exception)
{
    // do something
});

这篇关于Laravel 4:创建默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆