Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filters in Laravel 5

How do we make filters in Laravel 5? Is the idea of filters going away?

like image 905
JoshuaDavid Avatar asked Oct 15 '14 03:10

JoshuaDavid


People also ask

What is filters in laravel?

Filters give you the ability to tap into the routing lifecycle within Laravel and take actions based on various conditions both before and after the route request and response.

What does with do in laravel?

This with() method is used from a certain model which is specified inside this method. For example, if you have model called 'Country' and method called 'city', you would write Country::with('city') inside your controller or wherever you want. Shortly, it enables you to use method of a model.

Where are laravel models?

Models in Laravel 5.5 are created inside the app folder. Models are mostly used to interact with the database using Eloquent ORM.


2 Answers

The short answer is no, route filters are not going away entirely in Laravel 5.0 (despite some misleading information out there about this). The functionality still exists to let you use 'before' and 'after' filters on your routes if you would like. The "filters.php" file is no longer provided, but you can still define your filters somewhere else, probably most appropriately in the boot() function of Providers/RouteServiceProvider.php.

However, middleware is now the preferred way to achieve the same functionality. See http://laravel.com/docs/master/middleware for info about how to use it.

Middleware can be implemented to behave like either "before" or "after" filters. And it can be applied to all routes (called "global middleware"), or assigned to specific routes (by adding "'middleware' => 'auth'" for example to your route definitions in your routes.php file.

The only significant limitation of middleware is that it currently doesn't give you a way to pass parameters (as you can with filters). This means you can't do something like "requirePermission:admin" for example. There are currently two ways to deal with this limitation. You can instead just use a route filter instead, just as you did with Laravel 4.2. Or otherwise if you prefer using middleware, this feels like a bit of a hack, but you can pass parameters to the middleware by defining and retrieving custom values added to your route definition as explained at http://blog.elliothesp.co.uk/coding/passing-parameters-middleware-laravel-5/.

2015-05-29 Update: Middleware parameters are available starting with Laravel 5.1.

2015-06-10 Update: Route filters have been deprecated in preference of middleware and will be removed entirely with the release of Laravel 5.2 in December 2015.

like image 99
orrd Avatar answered Oct 13 '22 22:10

orrd


  1. Create a middleware with

    php artisan make:middleware class_name 
  2. Create a short-hand key in your app/Providers/RouteServiceProvider.php :

    protected $middleware = [   // ....   'shortName'  => 'App\Http\Middleware\class_name', ]; 
  3. You can now enable it to any Route (just like the L4 filters):

    $router->post('url', ['middleware' => 'shortName', function() {  ...  }]); 
like image 28
igaster Avatar answered Oct 13 '22 23:10

igaster