Hi guys, trying new unstable Laravel 5.
We have now the "Middlewares" and all default filters get migrated for it, all good until now.
Before this change, laravel had a FilterServiceProvider and my filters was registered there.
I had a filter that uses the current route to get a parameter, set it on session and them unset it from the parameter array, something like:
<?php namespace Hernandev\Blog\Http\Filters;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
class LocaleFilter
{
/**
* Run the request filter.
*
* @param Route $route
* @param Request $request
* @return mixed
*/
public function filter(Route $route, Request $request)
{
$lang = $route->getParameter('lang');
if ($lang && in_array($lang, config('app.locales'))) {
app()->setLocale($lang);
app('session')->set('lang', $lang);
$route->forgetParameter('lang');
} else {
$curr_lang = app('session')->get('lang', config('app.fallback_locale'));
app()->setLocale($curr_lang);
$route->setParameter('lang', $curr_lang);
}
}
}
and registered on a FilterServiceProvider
<?php namespace Hernandev\Blog\Providers;
use Illuminate\Foundation\Support\Providers\FilterServiceProvider as ServiceProvider;
class FilterServiceProvider extends ServiceProvider
{
/**
* The filters that should run before all requests.
*
* @var array
*/
protected $before = [
'Hernandev\Blog\Http\Filters\MaintenanceFilter',
];
/**
* The filters that should run after all requests.
*
* @var array
*/
protected $after = [
//
];
/**
* All available route filters.
*
* @var array
*/
protected $filters = [
'auth' => 'Hernandev\Blog\Http\Filters\AuthFilter',
'auth.basic' => 'Hernandev\Blog\Http\Filters\BasicAuthFilter',
'csrf' => 'Hernandev\Blog\Http\Filters\CsrfFilter',
'guest' => 'Hernandev\Blog\Http\Filters\GuestFilter',
'locale' => 'Hernandev\Blog\Http\Filters\LocaleFilter',
];
}
I don't know how to access the current route instance inside the middleware, since looks that the route has not been dispatched at the middleware runtime.
I may miss something obvious here, please help me get understand where i failed.
Many thanks in advance!
solved by using
in AppServiceProvider
protected $middleware = [
...
'locale' => 'Hernandev\Blog\Http\Middleware\LocaleMiddleware'
];
$router->group(
[
'middleware' => ['locale'],
],
function ($router) {
...
}
);
Looks like they dropped FilterServiceProvider. What does a filter class look like now?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community