middleware

 middleware provide a convienient way of filtering or inspecting -> http requests



mitddleware checks he is authenticared, verified  he will check that
it provides a convienent mechanism for inspectin g and filtering http requests entering your application


middlware means it scans the request like scanr, if finds any maliccious as per programm , it stops that reqwuest

location app/http/middleware

types of middleware

app/http/kernel.php

global midlware  protected $middleware = [

group midleware  protected $middlewareGroups = [

route middleware  protected $routeMiddleware = [


php artisan make:middleware name

it  handles an incoming request

parametrs are

* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed

middlware accepts request as object and next as parameters

ist perfomr som programme before passing to another middleare ussing next after pasing to

another midleare, accept this request return some response, it passes to next line

In Laravel middleware, a closure is a function that takes a request and a closure (which is commonly referred to as $next) as parameters, and it returns a response. The closure represents the next middleware in the pipeline that the current middleware should call. The $next variable is used to invoke the next middleware in the pipeline.

Here is an example of a simple middleware that demonstrates the use of a closure and $next variable:

php
<?php namespace App\Http\Middleware; use Closure; class ExampleMiddleware { ssss
public function handle($request, Closure $next) { // Perform actions before the next middleware is called // ... $response = $next($request); // Perform actions after the next middleware is called // ... return $response; } }

In this example, the handle method accepts a $request object and a $next closure as parameters. Inside the method, the middleware performs some actions before calling the next middleware using $next($request) and then performs some additional actions after the next middleware returns. Finally, the middleware returns the response generated by the next middleware.

The use of closures and the $next variable allows middleware to be chained together in a pipeline, with each middleware able to perform actions before and after the next middleware is called. This enables developers to add functionality to their applications in a modular and reusable way.


one of the example below

public function handle(Request $request, Closure $next) {
if (!Auth::user()) {
return $next($request);
} else{
Auth::logout();
return redirect()->route('admin.page-login');
}
}


GLOBAL MIDDLEWARE


u WANT ON EVERY HTTP REQUEST -USE MIDDLEWARE

register in app/http/kernel.php

register the particular middleware at particular group(route or grup or global)in ekernel php

for bootstrap 

official website getbootstrap.com search for what u required 

example;components, or b-card


in request or params if have country is india, so redirec tounavaliable 

means some country i have to block

in globlal middleware register











for global midleare-> no need of import at routes

, its automatically takes taht

url/greeting?country=india

work

url/greeting?country=srilanka


group MIDDLEARE

SOME TIMES you may want ot group several middlware under a single key to make them easier to asssign to routes

route::group([],callback);

Route::group(['middleware' => 'auth'], function () {
Route::prefix('smart_panel')->as('admin.')->group(
function ()
{
Route::view('/index', 'admin.dashboard')->name('dashboard');


new group in group middlwware


in web.php



url/user_profile?auth=1
then only works, withoutt auth not load
url/user_profile?auth=1&country=china
redirect to unavaible
both condition not satisfy-> global and groy

check auth middlware

route middleware


for single routs

route::get('/about',function(){


})->middleware('checkauth');

Route::get('/orders', [UiController::class, 'checkout_page'])->name('checkout_page');


Route::get('/orders', [UiController::class, 'checkout_page'])->name('checkout_page')->middleware('checkproduct');


g


checkproduct-> if product is there it will show otherwise redirect

url/product?product=software



]

not applicable one middlweare
withouytmiddleware

globalmiddlware we cannot exculeds

hw to use particular middleware in controller
in construct of controlller
class BankTransferController extends Controller
{
public function __construct()
{
$this->middleware(
[
'check_account_plan'
]
);
}



<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Plan;
use App\Models\User;

class EnsureACCOUNTPlanisthere
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$user = Auth::user();
$user_type = Auth::user()->type;
if($user_type == 'company' || $user_type == 'super admin')
{
$user = User::where('id', \Auth::user()->id)->first();
}
else
{
$user = User::where('id', \Auth::user()->created_by)->first();
}

if(!empty($user->plan)?Plan::find($user->plan)->account:'')
{

} else {
return redirect('login')->with('error', __('permission denied'));
}
return $next($request);
}
}



https://laravel.com/docs/8.x/controllers#controller-middleware
multiple middlwarea for a controller
class UserController extends Controller
{
/**
* Instantiate a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}
}

public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}


public function __construct()
{
$this->middleware(
[
'check_account_plan',
'auth'
]
);
}

laravel middlware assignment


  • _____ provide a convenient mechanism for inspecting and filtering HTTP requests entering in the application.

    Marked Answer : Middleware

    Correct Answer : Middleware

  • Middlewares are located in the ________ directory.

    Marked Answer : app/Http/Middleware

    Correct Answer : app/Http/Middleware

  • Which artisan command is used to create middleware?

    Marked Answer : make:middleware

    Correct Answer : make:middleware

  • Which method is used to prevent the middleware from being applied to an individual route within the group?

    Marked Answer : withoutMiddleware

    Correct Answer : withoutMiddleware

  • The middleware can be registered at ____________.

    Marked Answer : app/Http/Kernel.php

    Correct Answer : app/Http/Kernel.php



  • No comments:

    Post a Comment

    Event listening in react

     How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...