Setting up named routes within a subdomain group in Laravel 5.7

喜夏-厌秋 提交于 2020-02-23 07:10:00

问题


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?


回答1:


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>



回答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>


来源:https://stackoverflow.com/questions/53716958/setting-up-named-routes-within-a-subdomain-group-in-laravel-5-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!