The routing middleware classes implement the Middleware
interface:
<?php namespace IlluminateContractsRouting; use Closure; interface Middleware { /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next); }
In any class that implements this base class, there must be a handle
method that accepts the $request
as well as a Closure
.
The basic structure of a middleware is as follows:
<?php namespace IlluminateFoundationHttpMiddleware; use Closure; use IlluminateContractsRoutingMiddleware; use IlluminateContractsFoundationApplication; use SymfonyComponentHttpKernelExceptionHttpException; class CheckForMaintenanceMode implements Middleware { /** * The application implementation. * * @var IlluminateContractsFoundationApplication */ protected $app; /** * Create a new filter instance. * * @param IlluminateContractsFoundationApplication $app * @return void */ public function __construct(Application $app) { $this->app = $app; } /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($this->app->isDownForMaintenance()) { throw new HttpException(503); } return $next($request); } }
Here, the CheckForMaintenanceMode
middleware does exactly as its name suggests: the handle
method checks if the application is in application mode. The isDownForMaintenance
method of the app is called and if it returns true
, then a 503 HTTP exception will be returned and execution of the method will stop. Otherwise, the $next
closure with the $request
parameter is returned to the calling class.