我们可以使用多个具有多种二级域名吗?
我已经使用
例如;我有2个租户Company-A
和Company-b
,它们是通过Company-a.localhost
和Company> Company b的服务。 。
Company-A.Employee.localhost
,它应该告诉我 Company-A员工。
我尝试在subdoain
routeServiceProvider 的路由上使用以下内容:
Route::middleware('web')
->group(base_path('routes/security.php'));
Route::domain($this->baseDomain('admin'))
->middleware('web')
->name('admin.')
->group(base_path('routes/admin.php'));
Route::domain($this->baseDomain('employee'))
->middleware('web')
->name('employee.')
->group(base_path('routes/employee.php'));
private function baseDomain(string $subdomain = ''): string
{
if (strlen($subdomain) > 0) {
$subdomain = "{$subdomain}.";
}
return $subdomain . config('app.base_domain');
}
没有子域,它可以正常工作,但是具有第二级域的路由,它落在基本级域路由上并且没有当前的租户。 我在这里想念什么?这甚至可以实施吗?
谢谢。
I have implemented the simplest example using the Spatie docs for multitenancy, that is working perfectly fine. Now, I intend to use multiple second-level domains for each tenant I have.
For example; I have 2 tenants company-a
and company-b
and they are being served at company-a.localhost
and company-b.localhost
, now what I want is that when I visit company-a.admin.localhost
, it should tell me COMPANY-A ADMIN and If I visit company-a.employee.localhost
, it should tell me COMPANY-A EMPLOYEE.
I have tried using subdomain
on routes in RouteServiceProvider
like the following:
Route::middleware('web')
->group(base_path('routes/security.php'));
Route::domain($this->baseDomain('admin'))
->middleware('web')
->name('admin.')
->group(base_path('routes/admin.php'));
Route::domain($this->baseDomain('employee'))
->middleware('web')
->name('employee.')
->group(base_path('routes/employee.php'));
private function baseDomain(string $subdomain = ''): string
{
if (strlen($subdomain) > 0) {
$subdomain = "{$subdomain}.";
}
return $subdomain . config('app.base_domain');
}
Without subdomain, it works fine, but the routes with second-level domain, it falls to base level domain route and does not get the current tenant.
What am I missing here? Is this even possible to implement.
Thankyou.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以路由为例:
绑定字段将为
['subdoain','param1','param2']
,并且编译的路由将其宣布为>^( ?
但是,通过使用方法,这些正则是不可能的。您可以像在编译路线中那样声明上述路线
,Hostregex现在意味着
它将捕获
.example.com
之前的任何内容。如果您要求Company-A.Admin.example.com
,$ subdomain
将为company-a.admin
。您还可以在其域中声明具有两个绑定字段的路线:
如果您希望亚ubsubdomains暗示层次结构,这可能会更有用。
Take, for example, the route:
The binding fields would be
['subdomain', 'param1', 'param2']
, and the compiled route would have it's regexes declared asWhere
^(?P<subdomain>[^\.]++)\.
will explicitly stop capturing when finding a dot, in this case the delimiter between groups.However, these regexes are overridable by using the
where
method. You could declare the above route asIn the compiled route , the hostRegex would be now
Meaning it will capture anything preceding
.example.com
. If you requestedcompany-a.admin.example.com
,$subdomain
would becompany-a.admin
.You could also declare a route with two binding fields in its domain:
Which might be more useful if you wanted subsubdomains to imply a hierarchy.
我已经通过使用一些检查来实现这一目标,在
ruteserviceProvider
中,我没有在route> route
上使用实际的domain
函数,就像我们正常的IE路由:: domain('foo.bar')。原因是,
spatie
软件包使用一种中间件spatie \ MultitEnancy \ tenantfinder \ tenantfinder \ domaintenantfinder :: class
每当我们与房客comapny一起碰到域时,该> class
-a.localhost 。它从主机名中获得了租户IEcomapny-a.localhost
。在我的
routeserviceProvide
中:与我的情况一样,我只有这两种二级域,因此,我只是检查了主机名中是否存在此特定关键字,并相应地选择文件和中间件。
我还覆盖了
domaintenantfinder
类,在supterenancy.php
配置文件中:我已经实现了所需的结果,但是,我有一个安全问题,特别是在
routeServiceProvider中/代码>逻辑。想法??
I have achieved this by using some checks, in
RouteServiceProvider
, I have not used the actualdomain
function onRoute
like we do normally i.e.Route::domain('foo.bar')
. The reason was that, theSpatie
package use a kind of middlewareSpatie\Multitenancy\TenantFinder\DomainTenantFinder::class
which runs whenever we hit the domain with tenantcomapny-a.localhost
. And it gets the tenant from hostname i.ecomapny-a.localhost
.In my
RouteServiceProvide
:As In my scenario, I had only these 2 kind of second-level domains and so, I just checked if this particular keyword exists in the hostname and choosing the file and middleware accordingly.
I also overrided the
DomainTenantFinder
class and inmultitenancy.php
config file:I have acheived the desired outcome, however, I have a security concern, specially in
RouteServiceProvider
logic. Thought??