middleware provide a convienient way of filtering or inspecting -> http requests
mitddleware checks he is authenticared, verified he will check that
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
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
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);
for single routs
route::get('/about',function(){
})->middleware('checkauth');
hw to use particular middleware in controller
_____ 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