3

I have been working on a multi-tenant app and I'm trying to set up the routes in sub-domains according to the documentation:https://laravel.com/docs/5.7/routing#route-group-sub-domain-routing

In my web.php route file, I have something like this:

Route::domain('{account}.example.test')->group(function () {        
    Route::get('/home', 'HomeController@index')->name('home');        
});

Right now, the problem is using named routes in blade, but I suppose I may run into the same problem eventually in my Controllers.

Whenever I try to used a named route like this:

Blade Code

<a href="{{ route('home') }}">Home</a>

I get the following error:

Missing required parameters for [Route: home] [URI: home]. (View: /home/vagrant/Code/example/resources/views/example.blade.php)

I have found a way for solving this, you just have to:

<a href="{{ route('home', request('account')) }}">Home</a>

I also "solved" this using a helper...

if (! function_exists('acctRoute')) {
    function acctRoute($name = '')
    {
        return route( $name, request('account'));
    }
}

So I can use it like:

<a href="{{ acctRoute('home') }}">Home</a>

But I'm still wondering if there's a cleaner way to do this, maybe with some middleware that always injects the parameter?

Erubiel
  • 2,934
  • 14
  • 32

2 Answers2

11

This is my answer to my own question in case anyone needs this in the future:

From here I noticed that you can set default values for all routes under a middleware: https://laravel.com/docs/5.7/urls#default-values

So... This is what I ended up doing

First create the middleware:

php artisan make:middleware MyMiddleware

Then update the handle method inside the created middleware as in the documentation example:

public function handle($request, Closure $next)
{
    URL::defaults(['account' => request('account')]);

    return $next($request);
}

Then register the Middleware in Kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'mymiddle' => \App\Http\Middleware\MyMiddleware::class,
];

Then use it as any other middleware in your routes files:

Route::domain('{account}.example.test')->middleware('mymiddle')->group(function () {        
    Route::get('/home', 'HomeController@index')->name('home');        
});

And finally, use the route helper function as usual:

<a href="{{ route('home') }}">Home</a>
Erubiel
  • 2,934
  • 14
  • 32
2

You could share the account (subdomain) variable across all views:

// AppServiceProvider

public function boot()
{
    View::share('subdomain', request('account'));
}

// blade
<a href="{{ route('home', $subdomain) }}">Home</a>

Another approach could use a service container binding:

// AppServiceProvider
$this->app->bind('account.route', function () {
    return route('home', request('route'));
});

// blade
<a href="{{ app('account.route') }}">Home</a>
Brian Lee
  • 17,904
  • 3
  • 41
  • 52