Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: Middleware before & after into routes

I have two Middlewares: beforeCache & afterCache, boths registered on Kernel.

I want to call them into routes in this order: 1. beforeCache 2. myController 3. afterCache

If I define a route like this:

Route::get('especies/{id}', [
    'middleware' => 'beforeCache', 
    'uses' => 'MyController@myMethod', 
    'middleware' => 'afterCache', 
]);

beforeCache don't executes because afterCache is redefining the same array key middleware.

How should I do that? Thanks!

like image 286
Federico González Brizzio Avatar asked Jul 09 '15 15:07

Federico González Brizzio


People also ask

What is before and after middleware in Laravel?

For Before middlewares you do your code and return the next request after your code executes. For After middleware you handle the rest of the request and then your code executes and finally return the response.

What is default middleware in Laravel?

Laravel framework comes with several middlewares, including midlewares for — authentication and CSRF protection. The default middleware are located in the app/Http/Middleware directory.

How many types of middleware are there in Laravel?

There are two types of Middleware in Laravel. The Global Middleware will run on every HTTP request of the application, whereas the Route Middleware will be assigned to a specific route. The middleware can be registered at app/Http/Kernel.

What is middleware in Laravel 8?

Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated.


2 Answers

I'll assume you're using 5.1 in this, but what you're doing is essentially trying to define an array of attributes on the route. The brackets [] are just a shorthand version of saying array(...).

From the documentation (http://laravel.com/docs/5.1/middleware#defining-middleware) specifically the Before / After Middleware you simply just need to return a certain way.

For Before middlewares you do your code and return the next request after your code executes.

public function handle($request, Closure $next)
{
    // Perform action

    return $next($request);
}

For After middleware you handle the rest of the request and then your code executes and finally return the response.

public function handle($request, Closure $next)
{
    $response = $next($request);
    // Perform action
    return $response;
}

The route would end up looking like this,

Route::get('especies/{id}',[
    'middleware' => [
        'beforeCache',
        'afterCache'
    ],
    'uses' => 'MyController@myMethod'
]);
like image 184
Sieabah Avatar answered Sep 19 '22 12:09

Sieabah


class BeforeMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        // Do Stuff
        return $next($request);
    }

}

class AfterMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        $response = $next($request);
        // Do stuff
        return $response;
    }

}

1-The before middleware operates and then passes on the request.

2-The after middleware allows the request to be processed, and then operates on it

like image 42
Mahendra Avatar answered Sep 17 '22 12:09

Mahendra