Laravel 4:创建默认路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18942646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4: Create a default route
提问by Stefan S
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.
我试图在我的 Laravel 4 REST API 中创建一个默认路由,当我的其他定义的路由都没有匹配请求时,它会命中,以向调用者返回一个特定的错误。
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 定义,routes.php
但这不起作用。
Route::when("*", function(){
throw new CustomizedException('Route not found...');
});
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}}
当我有这条路线并做一个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}}
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}}
调用不存在的路由不会触发我自定义的异常,而是标准异常:
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1429}}
I also tried using any
as suggested in this post, but that also doesn't work:
我也尝试any
按照这篇文章中的建议使用,但这也不起作用:
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.
任何提示,我做错了什么,将不胜感激。
回答by ivanhoe
Took me a while to figure this out, actually @devo was very close:
我花了一段时间才弄清楚这一点,实际上@devo 非常接近:
Route::get('{slug}', function($slug) {
// check your DB for $slug, get the page, etc...
})->where('slug', '^.*');
This will catch the / too. If you'd rather handle the home page separately change the regexp to: where('slug', '^.+');
这也将捕获 / 。如果您更愿意单独处理主页,请将正则表达式更改为: where('slug', '^.+');
回答by pusch
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).
如果没有匹配的路由,Laravel 会抛出一个“Symfony\Component\HttpKernel\Exception\NotFoundHttpException”。您可以简单地编写自己的错误处理程序来捕获该异常并执行其他操作(查看http://laravel.com/docs/errors)。
Add one of the following two blocks (e.g. in your "app/start/global.php" in the "Application Error Handler" block):
添加以下两个块之一(例如,在“应用程序错误处理程序”块中的“app/start/global.php”中):
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
// do something
});
Or:
或者:
App::missing(function($exception)
{
// do something
});
回答by devo
Try something like this,
尝试这样的事情,
Route::get('{slug}', function($slug) {
// get the page from database using Page model
$page = Page::where('slug', '=', $slug)->first();
if ( is_null($page) ) {
return App::abort(404);
}
return View::make('page')->with('page',$page);
});
// Show 404 Page
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
回答by Sebastien Horin
Try to put this at the end of your route file?
尝试将其放在路由文件的末尾?
Route::any( '/{default?}', function( $page ){
throw new ValidationException('Custom error');
});
Or
或者
Route::any('/{default?}', 'TestController@yourMethod'); // pointing to a method displaying a 404 custom page