Laravel 9重写Auth to Ajax |寄存器()函数在哪里?

发布于 2025-02-06 16:43:09 字数 1657 浏览 0 评论 0原文

我使用的是Laravel 9,并且使用他们的验证系统,例如注册/登录等。 我目前正在尝试了解它的确切工作原理,并且我被困在这里:

如果我观察所有验证路线,那么我会看到注册用户的函数称为“寄存器”,并且可以在auth \ registercontroller中找到:

POST register .......... Auth\RegisterController@register

现在在他们的寄存器controller中,没有功能“寄存器”。那是他们的整个班级:

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct() {
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data) {
    return Validator::make($data, [
        'username' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data) {
    return User::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'role' => 'user',
        'created_at' => Carbon::create(date("Y-m-d H:i:s")),
    ]);
}

我已经有一个很好的功能,它效果很好。它创建一个用户。我也可以通过JavaScript重定向,这就是这样,但是我想了解系统的工作原理。

我也不需要服务器上的验证器,因为我在前端上进行验证,并且DB处理其余的验证器。

另外,我想查看错误的情况下JSON响应,以便我可以向用户显示一条消息,而是获得422(无法处理的内容)

I am using Laravel 9 and I use their auth system, e.g. register/login etc.
I am currently trying to understand how exactly it works and I am stuck here:

If I observe all the auth routes, then I will see that the function which registers a user is called "register" and can be found in Auth\RegisterController:

POST register .......... Auth\RegisterController@register

Now in their RegisterController there is no function "register". That is their whole class:

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct() {
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data) {
    return Validator::make($data, [
        'username' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data) {
    return User::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'role' => 'user',
        'created_at' => Carbon::create(date("Y-m-d H:i:s")),
    ]);
}

I have already an AJAX function which works great. It creates a user. I could also redirect via javascript and thats it, but I would like to understand how the system works.

I also do not need the validators on the server as I do the validation on the frontend and the DB handles the rest.

Also I want to see in case of an error the JSON response so I can display a message to the user, instead I get 422 (Unprocessable Content)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2025-02-13 16:43:09

您在该类中找不到该方法,因为它是在

寄存器特征上。

在顶部注意:使用RegistersUser

在这里您是该方法实时的该文件的源代码。

Yo can not find that method in that class because it is on

RegisterUsers Trait.

Notice at top : Use RegistersUser

Here you are the source code of that file in which that method live.
RegistersUsers.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文