Kieran Headley
Kieran Headley

Reputation: 667

Passing variables into middleware Laravel

I am using wildcard subdomains and want to pass the $element variable into the middleware whitelabel so I can check the subdomain and respond accordingly.

Route::group(['domain' => '{element}.website.co', 'middleware' => 'whitelabel'], function() {

    Route::get('/', 'AuthController@getLogin');
    Route::post('/', 'AuthController@postLogin');

});

How would I use the value of element within the middleware?

Upvotes: 1

Views: 308

Answers (1)

Rwd
Rwd

Reputation: 35220

Firstly, (unless you already have done) you'll need to add the following to your:

Route::pattern('element', '[a-z0-9.]+');

You can add it to the boot() method of your AppServiceProvider.

Then to access it in your middleware you would have something like:

public function handle($request, Closure $next)
{
    $domain = $request->route('element');

    return $next($request);
}

Hope this helps!

Upvotes: 1

Related Questions