Laravel将.html附加到路由(也可以在没有.html的情况下使用) [英] Laravel appending .html to route (and also have it work without the .html)

查看:95
本文介绍了Laravel将.html附加到路由(也可以在没有.html的情况下使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加.html并使其同时起作用的最简单方法是什么?

What would be the easiest way to add .html and have it work both ways?

example.com/about->有效!

example.com/about -> works!

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

example.com/about.html -> works!

我可以在路由中添加".html",但是如果没有,它将无法正常工作.

I can add ".html" to the route, but then it doesn't work without.

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

推荐答案

尝试以下方法:

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

如果您有许多需要此模式的页面(感谢@Mike),也可以使用RouteServiceProvider来捕获扩展名:

You can also use RouteServiceProvider to catch the extension if you have many pages that needs this pattern (thanks @Mike) :

//app/Providers/RouteServiceProvider.php

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

,然后在您的route.php

and then in your routes.php

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

这篇关于Laravel将.html附加到路由(也可以在没有.html的情况下使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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