Support the ongoing development of Laravel.io →
posted 10 years ago
Requests

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!

Last updated 2 years ago.
0

solved by using

in AppServiceProvider

protected $middleware = [
        ...
        'locale' => 'Hernandev\Blog\Http\Middleware\LocaleMiddleware'
    ];
$router->group(
    [
        'middleware'   =>  ['locale'],
    ],
    function ($router) {
        ...
    }
);

Last updated 2 years ago.
0

Looks like they dropped FilterServiceProvider. What does a filter class look like now?

Last updated 2 years ago.
0

Yes, I'm wondering the same thing!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

hernandev hernandev Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.