An example of "before" middleware would be as follows:
<?php
namespace App\Http\Middleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
// Perform action
return $next($request);
}
}
while "after" middleware would look like this:
<?php
namespace App\Http\Middleware;
use Closure;
class AfterMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
// Perform action
return $response;
}
}
The key difference is in how the $request
parameter is handled. If actions are performed before $next($request)
that will happen before the controller code is executed while calling $next($request)
first will lead to the actions being performed after the controller code is executed.