简体   繁体   English

如何通过 url 到 Controller 在 Laravel

[英]How to Pass slugged url to Controller in Laravel

Hey Guys I am new to Laravel and I am currently trying to pass a slugged url into my controller, but I am having issues with it.嘿伙计们,我是 Laravel 的新手,我目前正在尝试将一个 slugged url 传递到我的 controller 中,但我遇到了问题。

I have a table for categories and I have all the categories looped through and displayed on a view page (/posts).我有一个类别表,所有类别都循环播放并显示在视图页面(/帖子)上。 I listed all the categories on this view, so that when a user clicks on a category, they will be taken to a different page showing all posts belonging to that category(eg, /posts/category/{categoryID}).我在此视图中列出了所有类别,因此当用户单击某个类别时,他们将被带到显示属于该类别的所有帖子的不同页面(例如,/posts/category/{categoryID})。 I know I can get this done by passing the category id to the controller and finding the id to pass back to that view.我知道我可以通过将类别 id 传递给 controller 并找到要传递回该视图的 id 来完成此操作。 However, I am trying to do something different.但是,我正在尝试做一些不同的事情。 I am looking to pass the category name instead of the id to the controller.我希望将类别名称而不是 id 传递给 controller。 Eg, /posts/category/{category name}.例如,/posts/category/{类别名称}。 But due to the fact that some categories are more than a word (eg, Dry Cleaning), it has become a challenge for me.但是由于某些类别不仅仅是一个词(例如,干洗),这对我来说已经成为一个挑战。 I had earlier slugged the URL for SEO purpose (eg, /posts/category/{Str::str(categoryname)}) and from the web.php file, I created a get route.我之前曾出于 SEO 目的(例如 /posts/category/{Str::str(categoryname)})和 web.ZE1BFD762321E409CEEC4AC06 文件创建了一个获取路径的 URL。

Now, I'd like to fetch all posts relating to the category name passed in. The issue is that the category names are slugged.现在,我想获取与传入的类别名称相关的所有帖子。问题是类别名称被塞住了。

Is there a way to remove the slug("-" or hyphen) from the name passed into the controller?有没有办法从传递给 controller 的名称中删除 slug(“-”或连字符)? So, instead of "Dry-cleaning", it will come back as "Dry cleaning" which matches the name in the database.因此,它会以与数据库中的名称匹配的“干洗”形式返回,而不是“干洗”。

I am trying to avoid passing id to the url.我试图避免将 id 传递给 url。

Thanks.谢谢。

Good question.好问题。

There are a couple of approaches you can use to tackle this problem.您可以使用几种方法来解决此问题。 I'll run through them for you.我会为你遍历它们。

1. Store the slug in the database 1. 将 slug 存储在数据库中

One option is to store the slug name within the database.一种选择是将 slug 名称存储在数据库中。 Personally this is my favourite option as you don't need to do any string transformations.就个人而言,这是我最喜欢的选项,因为您不需要进行任何字符串转换。 With this approach I would put a slug column in your categories table.使用这种方法,我会在您的类别表中放置一个slug列。 Then you can use the Category eloquent model to search for a category with that slug.然后,您可以使用类别 eloquent model 来搜索具有该 slug 的类别。

2. Convert the slug to title case 2. 将 slug 转换为标题大小写

The second option is to convert the slug to title case.第二种选择是将 slug 转换为标题大小写。 You can do this by using Laravel's string class.你可以使用 Laravel 的字符串 class 来做到这一点。 For example:例如:

$categoryName = Str::title($slug);

However there are a few issues with this approach.但是,这种方法存在一些问题。 Firstly, when creating a slug the url will remove characters that are not safe, for example an apostrophe.首先,在创建 slug 时,url 将删除不安全的字符,例如撇号。 This means if you have a pluralised category name it won't match when converting the slug back to title case.这意味着如果您有一个复数类别名称,则在将 slug 转换回标题大小写时它将不匹配。

Secondly, by using code to transform strings in order to match a record in the database you are at the mercy of the service class.其次,通过使用代码转换字符串以匹配数据库中的记录,您将受制于服务 class。 While the functionality of Str::title is unlikely to change, it could in theory which could then mean you have to change code later on down the line.虽然Str::title的功能不太可能改变,但理论上它可以改变,这可能意味着您必须在以后更改代码。

Conclusion结论

Overall, I would recommend going with the first approach.总的来说,我建议使用第一种方法。 You have much better data integrity and less chance of things going wrong further down the line.您的数据完整性要好得多,以后出现问题的可能性也较小。 So if I were you I would create a new migration to add a slug column to your categories table and then populate each category with its own slug.因此,如果我是您,我将创建一个新的迁移以将slug列添加到您的类别表中,然后用自己的 slug 填充每个类别。 Then search for a category based on the slug column when querying your eloquent model.然后在查询您的 eloquent model 时根据slug列搜索类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM