Laravel 5: Multi-level authorization: coding explained – registration

This page describes Student authentication layer. But since other (Associate, Tutor) are pretty much same, this serves as an explanation for them as well.




When we clicked on Student gateway link on previous page and then we selected button “Register”, our request went to routes.php file which acting as a switchboard pulled file: app\Http\Routes\StudentAuthRoutes.php.

So, lets take a look at relevant code from routes file only.

Our auth controller mentioned above is located in:


Registration Form

So, you are going to AuthController and you think:
“Hey, there is no showRegistrationForm method!”

And you are right. It is not there.

But, when you look at AuthController, you will see all that stuff on the top;

One we are interested in looks like this:

If you click on it (in some good editor, like NetBeans) it will take you to a Trait, which a collection of classes, so you do not have write endlessly classes that usually come in bunches and they do often. Also, you can throw some methods into trait as well.
Of course, some coders feel now like they want to strangle me for Trait definition above.
Hopefully they’ll never find me ūüėČ

If you use some crapshoot of an editor, then find it by hand here:

And here is our Trait code:

We still do not have our method, but we are almost there.

If you click on RegistersUsers (using good editor) it will take you to a class, where finally we can get to our little lost method.

So, here is our missing method showRegistrationForm.

Of course, you can find our Trait RegistersUsers by hand here:


How AuthController can Access showRegistrationForm Method?

But you may say, that this method does not belong to our AuthController? And it does not extend Trait that method lives in?

Well, sort of it does.

AuthController uses AuthenticatesAndRegistersUsers:

Then trait AuthenticatesAndRegistersUsers uses RegistersUsers:

And in RegistersUsers our method lives.

And, if class uses class, they share resources of used class.
Like a good neighbors.
No, actually they would not stay a good neighbors for long as this¬†“using” goes only one way ūüėČ

But, we are not done yet.

Making Laravel Pull Our Custom Registration Form

If you look at our showRegistrationForm method, unless we set global variable $this->registerView, we will not get to our custom registration blade template, but to default: auth.register.

So, lets go back to our AuthController.

When you look at its construct method, you will find our protected $registerView variable set.

Again, no magic.

So, at this time, we already have our registration form starring back at our user:


Registering New User

This is route from /Routes/StudentAuthRoutes.php , which catches registration attempt:

So, lets find our register method.

Actually it resides in in same RegistersUsers.php trait, same as method showRegistrationForm.




I will take a shortcut here and just tell you that many different parts of Laravel working here to do validation.
I may not touch all elements, but most major are:

Whole process starts in RegistersUsers.php, method register()

Then data is being fed to register() method via AuthController:

Then we summon help of Factory method make() to do the work:

Factory is using some help from:

There is still error messages etc, but I stop it right here, as I am not writing Laravel tutorial.


Adding User to Database

Lets assume, we have our data validated and we can go to saving user and login him into the system.

Firstly, we are in RegistersUsers.php, method register().

Now, we are looking at this code:

Lets split it into parts.

This is our guard name: students.

Now, our system knows the guard, so it can attempt to create SessionGuard instance:

… using our guard name¬†and class SessionGuard, which can be found here:


SessionGuard class has login() method, which is chained to SessionGuard object:

SessionGuard ->login() method takes user instance as argument:

Our user method argument is a create() method of of our student AuthController:

array $data == $request->all(), which equals data from registration form:

Then it is processed by:

… in its method create():

As you can see, in comments, this method returns user (model) instance, which is used in SessionGuard->login() method:

And after this roller coaster, we can finally sit down and let our head unspin ūüėČ

I am not going to¬†go into messages at this point … as it makes little point, this not being Laravel tutorial.

Final Notes

So, without going any further into a full blown Laravel 5 tutorial, this is it  as far as login/logout is concerned.

You may consider reading about login and password reset.

Please go here and select proper link on the bottom.