Part III: Creating Admin Routes and Controller for our Multiple Authentication System in Laravel 5.4

In part II of this tutorial series, we created our admin model and also managed to set up our admin guard for our multiple authentication system. Today, we will continue with this series by setting up admin routes and controller. We have a lot today, so lets get into it.

To locate our route files we need to go to routes/web.php make sure web.php file has the following

On line where we have Route::get(‘/admin’,’AdminController@index’)->name(‘admin’); in web.php file we have a route that shows route admin. This route points to Admin controller where the method being used is index. We don’t have admin controller so let’s create it, shall we: To create AdminController.php open terminal and run the following command:

php artisan make:controller AdminController

After running the above command, Laravel creates our admin controller. Lets open our AdminController.php by navigating to app/Http/Controllers/AdminController.php Make sure your AdminController.php has the following code:

Now that we have our AdminController.php set up, lets explain the code. On line where we have $this->middleware(‘auth:admin’), we define our middleware and  tell AdminController.php which guard to use. In this case, we are going to use admin guard that we created in part II. On line where we have return view(‘admin’), we are returning a view called admin inside the method or function index.

We need to create this view, Laravel puts our views in resources/views. Inside our view folder, create admin.blade.php in this view, make sure it has the following code:

This view has just a normal bootstrap code, the only thing that is unique is line one which tells this blade to extends another layout. Laravel works on the principle of DRY(Don’t Repeat Yourself). Now we are set up, lets test our app. To run our application run the following command:

php artisan serve

Once your server is up and running open your browser and visit in you browser you should see the following:

To register as user click on register and you should have the following screen:

Enter you name, email address and password. Once your are done filling the form hit register. After you hit register, the system should take you to Pretty cool! Here is the screen shot:

Now we are logged in as a user, we can tell this by looking the dashboard, the system tell as we are logged in as user. We can further confirm this by the fact that the url which goes to web.php file and goes to this route Route::get(‘/home’, ‘HomeController@index’)→name(‘home’); which was auto-generated when we used php artisan make:auth

I think we are doing pretty well, so lets try to play with the system a little bit. At the top of your browser, change this URL to and hit enter on your keyboard. Laravel will redirect you back to because the admin guard is working. The reason that you are being redirected to is because you are still logged in as a user.

Actually what is happening behind the seen when you try to access admin route. Laravel goes to AdminController.php where we defined which guard to use. The system finds that you are not logged in as admin and you are redirected back.

In our next tutorial we will be running how to log in admins. To get the code visit Laravel 5.4 Multi Authentication System

Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy

Henry Mbugua

I am a Junior Developer ready to enter major leagues. My professional ambition is to stay versatile and be able to fill different roles within a product group. As such, I have been learning technology across frontend and backend.