1 / 6

LARAVEL PHP MIDDLEWARE

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.

ducathub
Download Presentation

LARAVEL PHP MIDDLEWARE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WELCOME TO DUCAT INDIA LARAVEL PHP MIDDLEWARE

  2. LARAVEL PHP MIDDLEWARE 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 

  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. Go to middleware folder and open “admin middleware file”. In admin middleware file you will find atom as -: “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. 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.

  6. THANK YOU CONTACT:- 70-70-90-50-90 info@ducatindia.com

More Related