简体   繁体   English

Laravel:如何在 URL 中使用用户 ID 以外的参数

[英]Laravel: How to use a parameter other than user id in a URL

I'm creating a Laravel web site where users can login and upload images.我正在创建一个 Laravel web 站点,用户可以在其中登录和上传图像。 For visitors to see that user's gallery, I've set up a gallery page with a URL like https://localhost8000/gallery/2, where 2 is the user ID.为了让访问者看到该用户的图库,我设置了一个带有 URL 的图库页面,例如 https://localhost8000/gallery/2,其中 2 是用户 ID。

My current code looks like this.我当前的代码如下所示。

web.php web.php

Route::get('/menu/{user}', 'CartController@menu');

CartController.php CartController.php

public function menu($user)
{
    $user = User::findOrFail($user);
    return view('new_menu')->with(['user' => $user]);
}

Instead of using the user ID as a route parameter, I want to use a random string.我不想使用用户 ID 作为路由参数,而是使用随机字符串。 Currently a random string of 32 digits is created when the user registers and stored in the users table in a column called random .目前,当用户注册时会创建一个 32 位的随机字符串,并将其存储在users表中名为random的列中。

I couldn't figure out how to relate "random" to "user" in web.php and Controller.我无法弄清楚如何将 web.php 和 Controller 中的“随机”与“用户”联系起来。

When using route model binding (which you should always be doing) Laravel handles the lookup for you.使用路由 model 绑定(您应该始终这样做)时,Laravel 会为您处理查找。 For example:例如:

public function menu(User $user)
{
    return view('new_menu')->with(['user' => $user]);
}

No lookups needed, and Laravel will automatically handle 404s for you.无需查找,Laravel 会自动为您处理 404。

As mentioned in comments, you can also specify the key used to lookup the model instance:如评论中所述,您还可以指定用于查找 model 实例的键

Route::get('/menu/{user:random}', 'CartController@menu');

This presumes that the random column has a UNIQUE index, otherwise results may be unpredictable.这假定random列具有UNIQUE索引,否则结果可能无法预测。

ok first, something is wrong with your route.好的,首先,您的路线有问题。 you said you want users to visit "gallery/2" for user id=2 gallery of images right?您说您希望用户访问“gallery/2”以获取用户 id=2 的图片库,对吗? but instead of 2, you want to use a 32 char string to find the user.但不是 2,您想使用 32 个字符的字符串来查找用户。 just do this:这样做:

Route::get('/gallery/{userString}', 'CartController@gallery');


public function gallery($userString)
{
    $user = User::where('random',$userString)->first();
    if($user == null)
      // return some error about user not found

    return view('new_menu')->with(['user' => $user]);
}

always rememberm it does not really matter what you pass in the route as a parameter, it could be any number or string.永远记住,你在路由中传递什么作为参数并不重要,它可以是任何数字或字符串。 its about what you want to do with it in the controller.它是关于你想在 controller 中用它做什么。 just like the code above which we search for a user who has a matching random column with the $userString就像我们在上面的代码中搜索具有与$userString匹配的random列的用户一样

First, add in your user migration file the field:首先,在您的用户迁移文件中添加以下字段:

'user_gallery_id' => Illuminate\Support\Str::random(32);

Then in your route for https://localhost8000/gallery/2:然后在 https://localhost8000/gallery/2 的路线中:

Route::get('/gallery/{user_gallery_id}', function($user_gallery_id) {
  return User::where('user_gallery_id', $user_gallery_id)->with('gallery')->get();
});

I don't know your Controller and Models.我不知道你的 Controller 和型号。 You can edit and customise it.您可以编辑和自定义它。 But that is roughly the worklfow for this usecase.但这大致是这个用例的工作流程。 I just did it with a closure now.我现在只是关闭了它。 You can edit and customise it.您可以编辑和自定义它。 But that is roughly the worklfow for this usecase.但这大致是这个用例的工作流程。 I just did it with a closure now.我现在只是关闭了它。 I assume that Gallery and User are already in relation to each other.我假设 Gallery 和 User 已经相互关联。

In your blade file you have the access to the gallery: @dd($user->gallery)在您的刀片文件中,您可以访问图库: @dd($user->gallery)

You will have to do a route model binding, you can do something like this in the model you want the URL to have other data, eg if you want the username instead of id, in the user model you will add this line of code. You will have to do a route model binding, you can do something like this in the model you want the URL to have other data, eg if you want the username instead of id, in the user model you will add this line of code.

/**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'username';
    }

Note whatever value you are returning must be an existing value in the user table.请注意,您返回的任何值都必须是用户表中的现有值。

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

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