Hi guys,
In this blog,I can create custom middleware in laravel, we are able to discover ways to create custom middleware in laravel utility and the way to use middleware in laravel based mission. truely create one custom middleware and take a look at language in query string.
clearly laravel middleware filter out all of the http request in laravel based totally tasks. as an instance when user is do any request that point middleware take a look at person is loggedin or now not and redirect therefore. Any person is not loggedin but he need to get entry to to dashboard or different things in projects that point middleware filter request redirect to consumer.
on this laravel middleware educational, we can supply a example of active or inactive users. Now we can name middleware in routes to restriction logged person to get right of entry to that routes, if he/she is blocked by way of admin.
In this laravel middleware tutorial, we will give a example of active or inactive users. Now we will call middleware in routes to restrict logged user to access that routes, if he/she is blocked by admin.
Step:1 Create MiddlewareIn this step, We have to create custom middleware in laravel based project. So let’s open your command prompt and run below command :
php artisan make:middleware CheckStatusStep:2 Register Middleware
After successfully create middleware, go to app/http/kernel.php and register your custom middleware here :
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 = [ .... 'checkStatus' => \App\Http\Middleware\CheckStatus::class, ]; } ?>Step:3 Implement logic In Middleware
After successfully register your middleware in laravel project, go to app/http/middleware and implement your logic here :
app/Http/Middleware/CheckStatus.php<?php namespace App\Http\Middleware; use Closure; class CheckStatus { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (auth()->user()->status == 'active') { return $next($request); } return response()->json('Your account is inactive'); } } ?>Step:4 Create Route
In this step, simply we will create a route and use custom middleware here. Filter http every request and protect routes :
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HomeController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::middleware(['checkStatus'])->group(function(){ Route::get('home', 'HomeController@home'); });Step:5 Add Method in Controller
Now we will create one method name language and let’s check :
app/Http/Controllers/HomeController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { public function home() { dd('You are active'); } } ?>
We have successfully create custom middleware in laravel based project with simple example. run the example.
It Will help you....
No comments:
Post a Comment