what are filters in laravel framework

 

 

 

 

 

 

 

 

what are filters in laravel framework and how to use them

what are filters in laravel framework and they are very important to any framework even web development processes.They can be used before or after route.basic filters are very important to any framework even web development processes.They can be used before or after route.But execution before or after would matter a lot and change the flow of programme.
<h3>new laravel filter</h3>
<p>basic use of filter</p>

<?php

// app/filters.php

Route::filter(‘mature’, function()
{
if (date(‘Y/d/m’) == ‘2000/01/01’) {
return View::make(‘mature’);
}
});

 

 

 
Of course, before the filter will become useful we need to attach it to a route. However, before we
can do that we need to change the route’s structure a little. Do you remember how I told you that
routing methods accept a closure as the second parameter? Well I told a little white lie again. Sorry.
You see the route methods can also accept an array as the second parameter. We can use this array
to assign additional parameters to the route. Let’s have a look at how a route looks with an array as
a second parameter.

for filter to work , we would connect it to route now open your app/routes.php file,
there you would see the below code

<?php

// app/routes.php

Route::get(‘/’, array(function()
{
return View::make(‘hello’);
}));
Very easily,you make a new route there as follows:
<?php

// app/routes.php

Route::get(‘/’, array( ‘before’ => ‘mature’,
function(){
return View::make(‘hello’);
}
));

 

In the above example, we are using mature filter to make it work !
Now go to app/filters.php
<?php

// app/filters.php

Route::filter(‘birthday’, function()
{
if (true) {
return View::make(‘mature’);
}
});

 

Now we can create after filter by following this code

<?php

// app/routes.php

Route::get(‘/’, array(
‘after’ => ‘mature’,
function()
{
return View::make(‘hello’);
}
));
same way we can create multiple filters by using laravel framework reuseability feature,
Multiple Filters
One other thing you should know is that you can apply as many filters as you like to a route. Let’s
have a look at some examples of this in action. First, let’s attach multiple before filters:
<?php

// app/routes.php

Route::get(‘/’, array(
‘before’ => ‘mature|gender-age|city’,
function()
{
return View::make(‘hello’);
}
));

we can use array instead of pipe as follows in the following example:
<?php

// app/routes.php

Route::get(‘/’, array(
‘before’ => array(‘mature’, ‘gender-age’),
function()
{
return View::make(‘hello’);
}
));

we can use before and after filer in same route as follows

<?php

// app/routes.php

Route::get(‘/’, array(
‘before’ => ‘mature’,
‘after’ => ‘gender-age’,
function()
{
return View::make(‘hello’);
}
));
Before filter would automatically work and after filter does after so.

 

 
filters can accept parameters. This is a great way to avoid repetition, and
allow for increased flexibility. Let’s lead with an example, as always.

<?php

// app/filters.php

// before

Route::filter(‘mature’, function($route, $request)
{

});

// after

Route::filter(‘mature’, function($route, $request, $response)
{

});

 
<?php
Route::filter(‘mature’,function($route,$request,$date)
{
if(date(‘d/m’)==$date)
{
return View::make(‘mature’);
}
});

Now we pass data through it
<?php
Route::filter(‘mature’,function($route,$request,$date)
{
if(date(‘d/m’)==$date)
{
return View::make(‘mature’);
}
});