简体   繁体   English

在Laravel 5.7的子域组中设置命名路由

[英]Setting up named routes within a subdomain group in Laravel 5.7

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 我一直在开发多租户应用程序,并且尝试根据文档在子域中设置路由: https : //laravel.com/docs/5.7/routing#route-group-sub-domain -路由

In my web.php route file, I have something like this: 在我的web.php路由文件中,我有类似以下内容:

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]. [Route:home] [URI:home]缺少必需的参数。 (View: /home/vagrant/Code/example/resources/views/example.blade.php) (查看:/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? 但是我仍然想知道是否有更清洁的方法来执行此操作,也许是使用总是注入参数的中间件?

You could share the account (subdomain) variable across all views: 您可以在所有视图中共享account(子域)变量:

// 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>

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 从这里,我注意到您可以在中间件下为所有路由设置默认值: 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: 然后,如文档示例中所示,在创建的中间件内部更新handle方法:

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

    return $next($request);
}

Then register the Middleware in Kernel.php 然后在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM