This document discusses middleware in Laravel PHP. It explains that middleware allows routes to be protected, and shows how to create a middleware called AdminMiddleware to check if a user is an admin. It describes adding an "admin" column to the users table via a migration. The document then demonstrates tying the AdminMiddleware to a route, so that route can only be accessed by admin users. It concludes by noting that more changes may be needed if the admin page is accessible directly without logging in.
2.
Middleware allows you to protect certain routes from being accessed. So, the
best way to kind of show it to you or explain it to you is by coding or creating
middleware by yourself.
First open code of your file has in routes folder and open web.php file in
which already authentication is provided. So when you access the file in
browser you will the login page first and in route you will the redirected
page after authentication of login. So this all happens in case of middleware
to protect the project
LARAVEL PHP
MIDDLEWARE
3.
But in url if you directly put “authentication.test/home” the you will see
that you are allowed to access it only if you login with credentials and this
will first check by middleware. If you not logged in and put direct url of
home then middleware redirect to login page.
Now try to create the own middleware.
It is done easily in Laravel using Php artisan tool as-:
>php artisan make: middleware AdminMiddleware (name)
And the middleware will successfully create.
– Now imagine user is an admin of your application and only they can
access a specific row. So to do such conditions.
4.
“Return $next ($request)” which says the return of next request.
So for this you have to add a new column in database i.e. 1 or 0 whether the
user is an admin or not.
Let’s go to terminal and run command – “>php artisan make: migration
add_admin_column_to_users_table” .
Now you can check migrations in database folder. Open the new migration
and make some alteration in code.
Go to middleware folder and open “admin middleware file”. In admin
middleware file you will find atom as -:
Now run the migration in console as-:
“php artisan migrate”
And now jump to database and check the users table. You will found
admin-0, let’s create new route and then you can protect this route as to
whether this user is an admin or not.
So open the routes- web.php and you were going to create just simple route
closure
5.
Now let’s go and change-:
– Open admin middleware file and in that file just simple write and run if
condition.
So in file- in the function writes if condition as-:
If (auth () ->guest()) as-:
If user is a guest then redirect to home page instead of login page. Because you
created the middleware but you don’t actually tie it to route, so to do that you can
actually go back to the routes in “web.php file”. Now define that you want
middleware of admin to be run before accessing this route as-:
Route::get (‘admin’, function () { Echo ‘you are an admin!’; }) ->middleware
(‘admin’);
So now you need to add middleware inside of kernel. Inside “kernel.php file” in
middleware, just add under the auth or get out at the end as mark below-:
Now check the admin middleware by run in browser and you will see the
login page of Laravel. Login with user credentials and check by open both
home. If admin page also opens directly without login privacy after login
for home page then you should make some more changes for admin.