Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

onmyown's avatar

Laravel Custom Registration Route/Logic

I would like to overwrite Laravel 5.2's public function postRegister() in my Auth controller.

I start by changing the route:

Route::post('/auth/register', 'Auth\AuthController@postRegisterI');

In my Auth controller, I now have this new postRegisterI() method as oppose to relying on the foundational postRegister(). But my changes in this new method don't seem to apply?

Is it still pointing to postRegister() for some reason? My AuthController looks like this:

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
     
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }
    
    public function postRegisterI(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        //Auth::login($this->create($request->all()));
        $this->create($request->all());
        //return redirect($this->redirectPath());
        return;
    }

    /**
     * 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, [
            'name' => 'required|max:255|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:2',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
} 

For example in my ajax requests to /auth/register/ it never echoes out my returns as I indicate and it also keeps trying to redirect me to /home. I'm doing this because instead of passing /auth/register a form, I'm passing it a Javascript array with data to not only create a user (email, password, etc) but also an application that the user needs to join.

Eventually, I would like to create the Application model with that data right when the User is created, but right now I'm not sure that my function is even being called correctly!

0 likes
7 replies
bestmomo's avatar

Where is new route regarding the other auth routes ?

onmyown's avatar

@bestmomo How do you mean?

It's at the bottom, with my other auth routes...

If the question is am I duplicating the route, that is not the case. There is only one route to /auth/register and it points to the new function. Nothing in the route file points to the foundational postRegister().

onmyown's avatar

It appears even after clearing the cahce, which seemed to work for a while, the problem is back... with a vengeance.

Any tips on this would be appreciated!

onmyown's avatar

Is it recommended to change the foundational function or point the route somewhere else?

I'm running out of ideas...

onmyown's avatar

So it is definitely hitting that route because when I comment out the new route the ajax request gets a 404, but when it is there, it doesn't point to the new function? Is AuthController.php cached somehow?

onmyown's avatar

So my custom returns only seem to work when I comment out

  public function __construct()
     {
    $this->middleware('guest', ['except' => 'getLogout']);
     }

From my AuthController... can anyone explain what this code does and why I need to comment it out to get my custom logic to run from that controller? It seems incorrect?

onmyown's avatar

Well I thought I had put it there but apparently it is in the source code.

Apply middleware guest to this controller, isn't that what that means? Except for the getLogout route...

I don't understand why this affects implementation of postRegisterI() also why are you affecting middleware in route getLogout from within this controller?

Please or to participate in this conversation.