简体   繁体   English

Laravel - 子域路由未按预期工作

[英]Laravel - subdomain routing not working as expected

I have three routing files inside routes folder:我在路由文件夹中有三个路由文件:

  • web.php网页.php
  • api.php api.php
  • admin.php管理文件

I've registered the file admin.php inside boot() method of RouteServiceProvider as following:我已经在RouteServiceProvider boot()方法中注册了文件admin.php如下:

public function boot() {
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::domain("admin." . env("APP_URL"))
            ->namespace($this->namespace)
            ->group(base_path("routes/admin.php"));

        Route::domain("api." . env("APP_URL"))
            ->middleware("api")
            ->namespace($this->namespace)
            ->group(base_path("routes/api.php"));

        Route::middleware("web")
            ->namespace($this->namespace)
            ->group(base_path("routes/web.php"));
    });
}

Let's say i've defined the following route in web.php:假设我在 web.php 中定义了以下路由:

Route::get("test", function() {
    return "Hello from website route";
});

If i try to access this route using mywebsite.com/test it acts like expected.如果我尝试使用mywebsite.com/test访问这条路线,它的行为就像预期的那样。

However, if i try to access the link admin.mywebsite.com/test it would still some how falls back to mywebsite.com/test and give the exact same output.但是,如果我尝试访问链接admin.mywebsite.com/test,它仍然会返回到mywebsite.com/test并给出完全相同的输出。

What's the problem here ?这里有什么问题?

Because web.php is not restricted to a domain.因为web.php不限于域。 As a consequence, the routes defined there are accessed by every domain that targets your laravel application.因此,每个针对您的 Laravel 应用程序的域都可以访问那里定义的路由。

If you only want the root domain to access the routes defined in your web.php , you could specify it in the RouteServiceProvider :如果您只希望根域访问web.php定义的路由,您可以在RouteServiceProvider指定它:

Route::middleware("web")
   ->domain(env("APP_URL")) // Add this line
   ->namespace($this->namespace)
   ->group(base_path("routes/web.php"));

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

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