Laravel optional prefix routes with regexp

后端 未结 4 971
野的像风
野的像风 2021-01-05 16:48

Is there a way to create routes with prefixes so I can have routes like this

/articles.html -> goes to listing  Controller in default language
/en/article         


        
4条回答
  •  没有蜡笔的小新
    2021-01-05 17:24

    There doesn't seem to be any good way to have optional prefixes as the group prefix approach with an "optional" regex marker doesn't work. However it is possible to declare a Closure with all your routes and add that once with the prefix and once without:

    $optionalLanguageRoutes = function() {
        // add routes here
    }
    
    // Add routes with lang-prefix
    Route::group(
        ['prefix' => '/{lang}/', 'where' => ['lang' => 'fr|en']],
        $optionalLanguageRoutes
    );
    
    // Add routes without prefix
    $optionalLanguageRoutes();
    

提交回复
热议问题