在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
在我的 web.php 路由文件中,我有类似以下内容:
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:
刀片代码
<a href="{{ route('home') }}">Home</a>
我收到以下错误:
缺少[Route:home] [URI:home]所需的参数. (查看:/home/vagrant/Code/example/resources/views/example.blade.php)
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?
推荐答案
这是我对自己的问题的回答,以防将来有人需要:
This is my answer to my own question in case anyone needs this in the future:
从这里我注意到,您可以为中间件下的所有路由设置默认值: https://laravel.com/docs/5.7/urls#default-values
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
首先创建中间件:
php artisan make:middleware MyMiddleware
然后按照文档示例中的说明更新所创建的中间件内部的handle方法:
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);
}
然后在Kernel.php中注册中间件
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>
这篇关于在Laravel 5.7的子域组中设置命名路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!