【问题标题】:Laravel Hide ID from urlLaravel 隐藏 url 中的 ID
【发布时间】:2019-01-11 14:42:20
【问题描述】:

我想从URL 中隐藏id,我有一个包含store_idstore_namestore_citystore_zipcode 字段的数据库表,现在它的工作方式如下:

http://example.com/store/1/Cafe-Crsip-Chicago-60640

但我希望它像这样改变:

http://example.com/store/Cafe-Crsip-Chicago-60640

URL 中不显示id

路线:

Route::get('store/{id}/{slug}',['as'=>'showProducts','uses'=>'FrontController@getProduct']);

HREF:

<a href="store/{{$store->store_id}}/{{str_replace(' ', '-', $store->store_name)}}-{{str_replace(' ', '-', $store->store_city)}}-{{$store->store_zipcode}}">

控制器:

 public function getProduct(int $id, string $slug)
 {
  $products = Product::where(['store_id'=>$id])
    ->orderBy('category_id', 'asc')
    ->with('category','relatedproducts', 'relatedproducts.product')
    ->get()
    ->groupBy('category_id');
 }

请注意,我使用 str_replace 删除 url 中的任何空格,所以如果我使用 store/{slug},如果我使用 str_replace 修改 url,则来自 url 的参数将不一样

【问题讨论】:

  • 您可能需要在表中添加另一行,例如“slug”,以便您可以查询。你想过这个方向吗?
  • 是的,最好在您的产品表中拥有一个唯一的 slug 列,这样您就可以通过其唯一的 url 分配单个产品
  • 使用许多可用的 sluggable 特征之一 - laracasts.com/discuss/channels/laravel/…
  • 更改 where 子句以按 slug 搜索并从路由和控制器参数中删除 id。特质包可能有助于一次性解决所有问题。
  • 最好在数据库表中创建新列slug 并将URL 更改为使用slug 而不是id 列。

标签: php laravel


【解决方案1】:

我所做的只是在 stores 表中添加了一个名为 slug 的新列 此 slug 将包含商店名称、城市和邮政编码(所有这些都不能重复)

Cafe-Crsip-Chicago-60640 这可以在添加具有小功能的新商店时添加,以将 3 个字段组合在一起并将其保存在字段 slug 中 在路线中添加了这个

 Route::get('{slug}',['as'=>'showProducts','uses'=>'FrontController@getProduct']);

在控制器中

  public function getProduct(string $slug)
  {

 $storesid = Storeinfo::where(['store_slug'=> $slug])->get()->first();

【讨论】:

    【解决方案2】:

    我会创建一条新路线:

    Route::get('store/{slug}',['as'=>'showProducts','uses'=>'FrontController@getProduct']);
    

    将 Href 更改为:

    <a href="store/{{str_replace(' ', '_', $store->store_name)}}-{{str_replace(' ', '_', $store->store_city)}}-{{$store->store_zipcode}}">
    

    请注意,我将 str_replace 中的 - 更改为 _

    然后更改控制器以查找 store_name、store_city 和 store_zipcode 而不是 store_id

    以下内容:

    public function getProduct(string $slug)
    {
        $slugs = explode("-", str_replace('_', ' ', $slug)); // replace the _ back to spaces and separate the 3 parts of the slug
        $products = Product::where([
                'store_name'=>$slugs[0], 'store_city'=>$slugs[1], 'store_zipcode'=>$slugs[2]
            ])
            ->orderBy('category_id', 'asc')
            ->with('category','relatedproducts', 'relatedproducts.product')
            ->get()->groupBy('category_id');}
    

    希望这会有所帮助!

    就像@waterloomatt 提到的,你也可以使用 laravel 提供的 slug helper 来做到这一点,但我不知道它是如何工作的。

    【讨论】:

    • 每个链接上的重定向可能不是性能和用户体验的最佳选择。一个简单的 slug 就可以解决这个问题。
    • @waterloomatt 我阅读了您发布的 laracast 的链接,我不知道您以后可以添加 slug。这是一个更好的解决方案。谢谢。
    • @BenoitRouleau 您的解决方案将不起作用,因为我还想使用 str_replace 删除空格以及为什么使用它来路由它将是相同的,您能解释更多如何修改 href 吗?
    • @Glove 如果要更改href,则不需要第二条路线。我将使用 href 的代码编辑我的答案
    【解决方案3】:

    您不能隐藏 URL 路径参数,而是重定向。你可以设置你的路由参数“id”可选

    Route::get('store/{id?}/{slug}',['as'=>'showProducts','uses'=>'FrontController@getProduct']);
    

    那么您的查询将不会获得任何 id 参数并且 你会得到类似“Laravel Type error: Too little arguments to function”的错误

    最好使用您的 slug 作为标识列,以避免使用 id。 那么你的路线将是

    Route::get('store/{slug}',['as'=>'showProducts','uses'=>'FrontController@getProduct']);
    

    并从控制器中删除 id 参数,然后使用 slug 进行查询。

    【讨论】:

    • store/{slug} 我知道它会起作用,但我没有使用 str_replace 的问题,如何使用 str_replace 删除 url 中的 %20?
    • 你可以使用函数 urldecode('your url');您将在此处找到详细信息 [php.net/manual/en/function.urldecode.php]
    • 我所做的只是将新列添加到名为 store_slug 的数据库中,其工作方式如下 $storesid = Storeinfo::where(['store_slug'=> $slug])->get()->first (); $products = Product::where('store_id',$storesid->store_id)
    • @Glove,如果这是解决问题的方法,请接受此答案
    • 这不是很好的解决方案,解决方案只是将 slug 添加到数据库中
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2015-04-12
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多