【问题标题】:how to hide id from url in Laravel 6?如何在 Laravel 6 中隐藏 url 中的 id?
【发布时间】:2021-10-31 03:19:28
【问题描述】:

从 url 隐藏 id

https://wallpaperaccess.in/photo/162/download-wallpaper

我想要这样的网址

https://wallpaperaccess.in/photo/download-wallpaper

ImagesController.php

public function show($id, $slug = null ) {

        $response = Images::findOrFail($id);

        $uri = $this->request->path();

        if( str_slug( $response->title ) == '' ) {

                $slugUrl  = '';
            } else {
                $slugUrl  = '/'.str_slug( $response->title );
            }

            $url_image = 'photo/'.$response->id.$slugUrl;

            //<<<-- * Redirect the user real page * -->>>
            $uriImage     =  $this->request->path();
            $uriCanonical = $url_image;

            if( $uriImage != $uriCanonical ) {
                return redirect($uriCanonical);
            }

路线

// Photo Details
Route::get('photo/{id}/{slug?}','ImagesController@show');

注意:我在数据库中没有任何 slug 列,所以我们可以使用 title 作为 slug 吗?

【问题讨论】:

  • 如果你想从 url 中隐藏 id,你应该将你的路由编辑为Route::get('photo/{slug?}','ImagesController@show');,然后你还必须将你的方法编辑为public function show($slug = null )。不确定这是否是您要查找的内容,因为看起来您正在使用 id 来查找图像?
  • 这是可能的Route::get('photo/{slug?}','ImagesController@show');id 值存储在会话中,并从会话Images::findOrFail(session()-&gt;get('id')); 获取到控制器上
  • 可以用uuid替换bigint id吗?
  • 很简单,你只需在 url 中加密和 dycript 你的 id
  • 我将我的路线编辑为Route::get('photo/{slug?}','ImagesController@show');,并将方法编辑为public function show($slug = null ),但它仍然显示 id 瞬间

标签: laravel routes


【解决方案1】:

您应该添加一个列字段slug 并从title 自动生成它

use Illuminate\Support\Str;

$slug = Str::slug($request->input('title'), '-');

Models\Image.php

public function getRouteKeyName()
{
    return 'slug';
}

routes\web.php

Route::get('photo/{image:slug}','ImagesController@show');

app\Http\Controllers\ImagesController.php

use app\Models\Image.php;

...
public function show(Image $image)
{
    // controller will automatically find $image with slug in url
    // $image_id = $image->id;
    return view('your.view', ['image' => $image]);
}

【讨论】:

    【解决方案2】:

    为了在 URL 中使用 slug 而不是 id,您需要...

    1. 在您的表中创建一个用于存储 slug 的列。使 slug 唯一的一个好方法是在末尾附加实际的 id。如果你真的不想在任何地方看到 id,你别无选择,你必须自己确保 slug 是唯一的(有很多方法可以实现这一点)。

    这是自动创建唯一 slug 的一种方法:

    确保slug 列可以为空,然后打开您的模型并添加这些方法。

    它们被称为“模型事件”。

    • created 在创建模型时调用。
    • updating 在您更新模型时调用,但在实际更新之前调用。

    使用createdupdating 应该会在您创建或更新Images 实例时自动创建slug。

        protected static function booted()
        {
            parent::booted();
    
            static::created(function (Images $images) {
                $images->slug = Str::slug($images->title.'-'.$images->id);
                $images->save();
            });
    
            static::updating(function (Images $images) {
                $images->slug = Str::slug($images->title.'-'.$images->id);
            });
        }
    
    

    从 SEO 的角度来看,当标题更改可能不是一个好习惯时更新 slug,所以你可能想省略这部分 (static::updating...),这取决于你。

    1. 转到您的模型并添加以下方法:
    /**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug'; //or whatever you call the slug column
    }
    

    这样,路由器将通过 slug 解析您的模型,而不是 id。

    1. 在您的路由文件中,删除 id 并更改 slug 的名称以匹配您的模型名称:
    Route::get('photo/{images}','ImagesController@show'); //here I'm assuming your model is Images from what I see in your controller
    
    1. 在您的控制器中,将 show 方法的声明更改为:
    public function show(Images $images)
    {
       dd($images);
    
    // if you did all this correctly, $images should be the Images corresponding to the slug in the url.
    // if you did something wrong, $images will be an empty Images instance
    //
    //
    // your code...
    }
    

    Images 应该重命名为Image,models 不应该是复数。不过,这里应该没什么区别。

    【讨论】:

    • 我创建了 slug 列。我需要自己一个一个往桌子上添加slug
    • 你可以做的是使用created事件。我会用一个例子来编辑我的答案。
    • 请以我为例
    • 我更新了我的答案,您可以找到一个如何自动创建和更新 slug 的示例。请注意,为了唯一性,我使用了 id。对于 SEO 而言,这不是问题,它是独一无二的,不会随着时间的推移而改变,而且在技术上也可以。如果您真的想使用其他东西,则必须自己实现此逻辑(例如,使用title 并在末尾附加uniqid(),类似这样。
    • 快速说明:重要的是 slug 列在您的数据库中可以为空,因为在我添加的示例中,您的 Images 将首先在没有 slug 的情况下添加,如果不是这样,您将遇到错误可能(不可为空)。
    猜你喜欢
    • 2021-01-06
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多