简体   繁体   中英

How can I create (register) an user while being logged in?

I defined an option in my website named "define user". So I need to do some changes in the current laravel registration system. Actually I did what I need:

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    if ( !isset($request->registered_by_id) ) {             -- added

        $this->guard()->login($user);

        $status = "";                                       -- added
        $msg = "";                                          -- added

    } else {                                                -- added
        $status = "define-user-alert-success";              -- added
        $msg = "the user registered successfully";
    }                                                       -- added

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath())
           ->with($status, $msg);                           -- added
}

As you know, function above is located under /vendor/laravel/framework/src/illuminate/Foundation/Auth/RegisterUsers.php path. The problem is all the change I made will be ignored when I upload my website and execute composer install command.

Any idea how can I keep my changes in the vendor directory?

composer install will overwrite your changes as it just fetches the latest version from the public repo and installs that.

I would suggest one of the following;

  1. Create your own fork of laravel and have composer load this over default laravel. I did this recently with a Symfony component fork, the idea is to change the repo branch name to your modified one and override the source with your own repo. Instruction from my fork are here .

  2. Upload the file manually via after executing composer install (not recommended, only use a stop-gap) .

  3. Override/extend the original class, this answer lays out the process nicely .

  4. As defined in this answer on Laracasts (which is very similar to your case), use event listeners to execute your code after user registration.

Hope this helps!

Yea, don't edit the file in vendor .

This trait is used in the RegisterController which exists in your app, not the framework. You can simply override that method in your controller, done.

Perhaps your changes can go in registered which is called after registration so you don't have to override this functionality of register itself (since registered is a blank method waiting to be overridden)

I would strongly recommend against making any changes to the core framework - aside from the issue you mentioned, it can also make upgrades extremely difficult.

Fortunately, Laravel makes user registrations easy. All you need to do is create a new controller (Eg UserController) and then use a function like this to create a model for them ...

public function registerUser(Request $request){
    $request->validate([
        'username' => 'bail|required|unique:users,username|max:255',
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required|email'
    ]);

    $user = User::create([
        'first_name' => $request->first_name,
        'last_name' => $request->last_name,
        'username' => $request->username,
        'password' => Hash::make($request->password),
        'email' => $request->email
    ]);

    return redirect('/settings/people/'.$user->user_id);
}

The key here is to use the hash facade to encrypt the password before committing the model to the database. Otherwise, it's essentially like working with any other model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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