Wednesday, 16 December 2015

Unknown

MiddleWare in Laravel Framework for filtering User Request .

Middleware IN LARAVEL FRAMEWORK


Middleware provides mechanism , a technique to filter a request made by user for application.
They are of various types such as Authentication,CSRF protection, maintainence middleware and we can create our own middleware also in order to filter a request by defining any constraints we want.
Simple example can be checking age whether user visiting site has age above 18.This can be achieved using a conecpt of Middleware.
Middleware are placed in app/http/middleware directory.

Now since we know how to use php artisan command line interface we can use it to create our own Middleware.

  • Step 1: Defining Middleware
    Run composer and type following command

    Now when you create middleware it will be place in app/http/middleware.
    I have created Oldmiddleware to check whether age taken as input from form entered is greater than 18.


    In the above snapshot handle() method is method in which we write a conditions that we want to apply on request,like here we hav applied a condition on input whose name is "age" in HTML by using $request variable.If entered data in age input is greater than or equal 18 then request proceeds to application other wise it is redirected to url specified in redirect('Url') method.
  • Step 2: Registering Middleware
    Now we have defined middleware, its time to register it in app/Http/Kernal.php file ,we can register it as : 


    1.Global Middleware
    Global Middleware are the one which are applied to entire application.They can be registered in $middleware array by adding ',' followed by middleware path in single quote '' as in  following code:
    protected $middleware=[,'path_of_middleware' ];

    2.Routing Middleware
    Routing Middleware are applied to specific rpute or  url eg. http://localhost/foo .They can be registered in $routeMiddleware array by adding ',' followed by 
    'alias_middelwarename' =>'middleware path'  as in  following code:
    protected $middleware=['ageauth'=>'OldMiddleware' ];


  • Step 3 : Applying middleware for any route
    Go to app/http/routes.php and add middleware for following  route

    Route::get('/old',['middleware'=>'alias_middleware_name',function()
    {
    return 'Success';
    }]);


    In the above code , we apply middleware for route for url '/old' using get request.
    middleware is called whenever we make request for the '/old' page if condition specified in middleware is met then we get text response "Success" on browser.

    So now u have understood the middleware and how to use it we will move on to next concept Controller .
  • Tips : Try some example to register a route and apply a middleware to it for more details or problems ore  write comment or visit laravel documentation
     Click here for Middleware concept


    Next Post is going to be on Controller which is key part in application where we apply business logic to data before data is saved into database.

Unknown

About Unknown -

Freelancer,Entrepreneur,Aesthetic bodybuilder.

    Knows :
  • Laravel Framework
  • Ionic Framework + Angular JS
  • Bootstrap
  • CSS
  • Jquery

Subscribe to this Blog via Email :