Laravel 5.5: Execute a method before registration

Deadly 提交于 2021-01-28 14:51:30

问题


I'm using the default laravel authentication. Every user who registeres in my site needs a activation pin. The valid PINs are stored in a different table. Whenever a user registeres, I want to test if the PIN is valid or not. So, is there any method that I can override in RegisterController that executes before regstering the user?


回答1:


Yes. You can override protected register method in RegisterController. This is a simple solution. I do this to validate params, save a new user, and force return JSON in one of my projects.

For example:

protected function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email|unique:users',
            'phone' => 'required',
            'pin' => 'required'
        ]);

        //Check your PIN here, if it's wrong, append errors array

        if ($validator->fails())
            throw new ValidationFailed($validator->errors());

        User::create([
            'first_name' => $request->input('first_name'),
            'last_name' => $request->input('last_name'),
            'email' => $request->input('email'),
            'phone' => $request->input('phone'),
            'password' => bcrypt(str_random(10)),
            'remember_token' => str_random(10),
        ]);

        return ResponseJSON::make(trans('responses.registered'));
    }



回答2:


You can add a validation rule for validating the pin in the validator method like this :

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'pin' => [
                    'required',
                    Rule::exists('pins')->where(function ($query) {
                        $query->where('Some condition');
                    }),
                  ],
    ]);
}

I assume you have a table for pins called pins




回答3:


extend the register function in user class:

public function register(Request $request)
    {
        event(new \App\Events\NewUserRegistered(Auth::user()));
        return redirect($this->redirectPath());
    }

NewUserRegistered:

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;

class NewUserRegistered extends Event
{
    use SerializesModels;

     public $userID;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $userID)
    {
        //get whatever is massed in here's value in the id column
        $this->id = $userID;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}


来源:https://stackoverflow.com/questions/47154625/laravel-5-5-execute-a-method-before-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!