MadzQuestioning
MadzQuestioning

Reputation: 3762

Pass parameter to Laravel Middleware

How can I passed a parameter in my middleware? I'm always getting this error enter image description here

Here are the structure of my middlware

<?php

namespace App\Http\Middleware;

use Closure;

class SubDomainAccess
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $subdomain)
    {
        dd($subdomain); // Just trying to output the result here
        return $next($request);
    }
}

And on the Kernel.php under the $routeMiddleware I added this

'subdomain.access' => \App\Http\Middleware\SubDomainAccess::class,

Now on my web.php route file I added this

Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
        Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing'])->middleware('subdomain.access');
});

Also I tried this

Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
    Route::group(['middleware' => 'subdomain.access'], function () {
        Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing']);
    });
});

I tried this but nothings working. The only thing I haven't tried is placing the middleware in my controller constructor. But I don't wan't it that way as I think this is messy and it's more elegant if its within the route file.

Hope you can help me on this. Thanks

Upvotes: 0

Views: 5509

Answers (3)

hizbul25
hizbul25

Reputation: 3849

Get URI from $request object and then return domain. No need to pass subdomain as params to middleware.

namespace App\Http\Middleware;

use Closure;

class SubDomainAccess
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $subdomain)
    {
        $sudomain = $this->getSubDomain($_SERVER['HTTP_HOST']);
        return $next($request);
    }

    /**
     * Get Subdomain name
     * @param $uri
     * @return bool
     */
    private function getSubDomain($uri)
    {
        if(!empty($uri))
        {
            $host = explode('.', $uri);
            if(sizeof($host) > 2)
                return $host[0];
        }

        return false;
    }
}

Upvotes: 0

MadzQuestioning
MadzQuestioning

Reputation: 3762

Ok so I managed to find a way to get the parameters without passing a third parameter on the middleware handle function thanks to this link

So what I did to retrieve the subdomain parameter is this

$request->route()->parameter('subdomain')

or if all parameter

$request->route()->parameters()

Upvotes: 2

cre8
cre8

Reputation: 13562

['middleware' => 'subdomain.access'] is wrong, try to use ['middleware' => 'subdomain:access'] with a : instead.

https://mattstauffer.co/blog/passing-parameters-to-middleware-in-laravel-5.1

Upvotes: 1

Related Questions