Laravel Auth Middleware Example Tutorial From Scratch

Today, We want to share with you Laravel Auth Middleware Example Tutorial From Scratch.In this post we will show you Laravel Middleware & Basic Auth Implementation, hear for Laravel 5.6 Create Custom Middleware example we will give you demo and example for implement.In this post, we will learn about Working With Controllers and Middleware in Laravel 5.6 with an example.

Laravel Auth Middleware Example Tutorial From Scratch

There are the Following The simple About Laravel Auth Middleware Example Tutorial From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel Middleware & Basic Auth Implementation, so the some major files and Directory structures for this example is following below.

Read Also:  Laravel 6 Auth Redirection Using redirectTo
  • Using Laravel 5 Auth Middleware
  • Custom Laravel Auth Middleware
  • Example 1 : Using Laravel 5 Auth Middleware

    To some restrict pages for users or any roles in your laravel project to check authentication (roles like user, admin, agent, manager)status, so include the laravel middleware to the controller’s constructor.

    app/Http/Controllers/MyController.php

    To restrict a all the methods or page to guests works only (not any signed in):

    public function __construct()
    {
        $this->middleware('guest');
    }
    

    For simple Authentication signed in users:

    public function __construct()
    {
        $this->middleware('auth');
    }
    

    If We need to make a some exceptions for certain this laravel controllers methods, so passing simple those parameters in as an array for next or the second argument.

    $this->middleware('auth', ['except' => 'show']);
    

    Example 2 : Custom Laravel Auth Middleware

    Step 1: Make a Laravel Custom Validation

    Working With Controllers and Middleware in Laravel

    php artisan make:middleware CheckAge
    

    app/Http/Middleware/CheckAge.php

    <?php
    namespace App\Http\Middleware;
    use Closure;
    class CheckAge
    {
        /**
         * Handle an incoming request.
         * //Using Laravel 5 Auth Middleware
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            if ($request->age != 25) {
                return response()->json('Please enter Your valid Age');
            }
    
    
            return $next($request);
        }
    }
    

    app/Http/Kernel.php

    <?php
    namespace App\Http;
    use Illuminate\Foundation\Http\Kernel as HttpKernel;
    
    class Kernel extends HttpKernel
    {
        ....
    
        /**
         * The application's route middleware.
         *
         * These middleware may be assigned to groups or used individually.
         *
         * @var array
         */
        protected $routeMiddleware = [
            ......
            ....
            //some regisert middleware
            'CheckAge' => \App\Http\Middleware\CheckAge::class,
        ];
    }
    

    Step 2: Add Route

    routes/web.php

    Route::get("test-age",["uses"=>"[email protected]","middleware"=>"CheckAge"]);
    

    Step 3: Add Laravel Controller Method

    app/Http/Controllers/MemberController.php

    <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    class MemberController extends Controller
    {
        public function testAge()
        {
        	dd('testAge');
        }
    }
    

    And then Last, now I am ready to run Laravel 5.5 Create Middleware example, Therefor We can run bellow simple links on your browsers and then check how custom my Live Middleware helper works.

    Read Also:  Laravel 7 User Login Authentication From Scratch

    http://localhost:8000/test-age?age=25
    http://localhost:8000/test-age?age=10
    

    Angular 6 CRUD Operations Application Tutorials

    Read :

    Summary

    You can also read about AngularJS, ASP.NET, VueJs, PHP.

    I hope you get an idea about Laravel Auth Middleware Example Tutorial From Scratch.
    I would like to have feedback on my Pakainfo.com blog.
    Your valuable feedback, question, or comments about this article are always welcome.
    If you enjoyed and liked this post, don’t forget to share.

    Read Also:  Create Authentication Custom Middleware in Laravel 5.7

    Related FAQ

    Here are some more FAQ related to this Article:

    1. Read Also:  Laravel 6 Custom Middleware Tutorial With Example
    2. Read Also:  PHP file_get_contents URL Examples
    3. Read Also:  Laravel middleware Multiple Roles and Parameters Example
    4. Read Also:  Login Signup with Laravel
    5. Read Also:  How to Get Yii last inserted ID from database
    6. Read Also:  PHP cUrl GET & POST Methods Example
    7. Read Also:  How to create a logfile in PHP
    8. Read Also:  Remove undefined value from jquery array
    9. Read Also:  How to Get current Logged User Details in Laravel 5.8?
    10. Read Also:  Laravel Auto redirect if not logged in
    CLOSEX