小编Geo*_*eld的帖子

Laravel 8 路由到控制器。SEO友好的URL结构

我试图弄清楚如何在 Laravel 8 项目中实现特定的 URL 结构以及实现这一目标的必要途径。我想要的是:

// Example urls to listings in the business directory.
// These urls should be routed to the directory controller.
www.domain-name.com/example-business-name-d1.html
www.domain-name.com/example-business-name-d15.html
www.domain-name.com/example-business-name-d100.html
www.domain-name.com/example-business-name-d123.html
www.domain-name.com/example-business-name-d432.html

// Example urls to articles/posts in the blog.
// These urls should be routed to the posts controller.
www.domain-name.com/example-post-name-p5.html
www.domain-name.com/example-post-name-p11.html
www.domain-name.com/example-post-name-p120.html
www.domain-name.com/example-post-name-p290.html
www.domain-name.com/example-post-name-p747.html

// We want to avoid the more traditional:
www.domain-name.com/directory/example-business-name-1.html
www.domain-name.com/blog/example-post-name-5.html
Run Code Online (Sandbox Code Playgroud)

这是因为我们不希望每个列表或博客文章的 url 中都包含字符串“directory”或“blog”。没有它,搜索引擎结果会更好。

到目前为止,我在 web.php 路由文件的底部使用了一个包罗万象的路由 {any} 来“捕获所有”到达那么远的路由。然后我操作路径提供的字符串以从 url 的末尾获取 ID 和单个字符标记。然后我有这两个变量,但可以弄清楚如何将它们传递给正确的控制器!

还是我真的很笨,有更好的方法来实现这一目标?

Route::get('{any}', function($any = null){ …
Run Code Online (Sandbox Code Playgroud)

php url url-routing laravel laravel-8

2
推荐指数
1
解决办法
948
查看次数

标签 统计

laravel ×1

laravel-8 ×1

php ×1

url ×1

url-routing ×1