Laravel将.html附加到路由中(也可以让它在没有.html的情况下工作)


Laravel appending .html to route (and also have it work without the .html)

添加.html并使其双向工作的最简单方法是什么?

example.com/about->works!

example.com/about.html->有效!

我可以在路由中添加".html",但如果没有它就无法工作。

Route::get('about.html', function () {
 return 'About page';
});

试试这个:

Route::get('about{extension}', function() {
    return 'About page';
})->where('extension', '(?:.html)?');

如果你有很多页面需要这种模式,你也可以使用RouteServiceProvider来捕获扩展(谢谢@Mike):

//app/Providers/RouteServiceProvider.php
public function boot(Router $router)
{
  $router->pattern('extension', '(?:.html)?');
  parent::boot($router);
}

然后在你的routes.php

Route::get('about{extension}', function() {
    return 'About page';
});
相关文章: