Laravel 4路由 - 如果不满足条件,则跳过路由的能力

时间:2014-10-23 10:50:24

标签: php laravel laravel-4 routing laravel-routing

因此我们在数据库中有大量内容,让我们调用这些文章。文章的路径从应用程序根开始,可以包含斜杠。所以我们想搜索数据库以查看我们是否匹配文章,如果没有跳过该路线并给其他路线提供回应的机会。

在Sinatra(我相信它激发了Laravel内部的路由)你可以选择pass to the next route。可能是这导致我误入歧途。

Route::get( '{uri}', function( URI $uri ) {
  // check database, if we find a record we'll need to pass it to the appropriate controller
  // we'll have a class that handles this responsiblity

  // pass if we don't find anything
} )->where('uri', '.*');

Route::get( 'other/page', 'OtherController@sayHello' );

问题Allow skip the route based on conditions #1899谈到这一点,虽然我看不到过滤器如何满足这个要求,但它们只是拦截并让你有机会停止路由执行(抛出异常,重定向到特定路由等) ,你不能没有错误return FALSE;。有一个example chaining a condition method to a route,虽然这种方法似乎不存在(在4.2中)。

有什么想法吗?

除此之外,我们还考虑在一个包中包含这个逻辑,以便它可以跨应用程序共享,并想知道你是否可以影响包提供的路由的执行顺序?

1 个答案:

答案 0 :(得分:0)

您可以拥有一组路线,这些路线包含在与其匹配的所有路线中。如果该组失败,则跳到下一部分。

Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter
    });

    Route::get('user/profile', function()
    {
        // Has Auth Filter
    });   
});

http://laravel.com/docs/4.2/routing

相关问题