繁体   English   中英

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

[英]Laravel - subdomain routing not working as expected

我在路由文件夹中有三个路由文件:

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

我已经在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"));
    });
}

假设我在 web.php 中定义了以下路由:

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

如果我尝试使用mywebsite.com/test访问这条路线,它的行为就像预期的那样。

但是,如果我尝试访问链接admin.mywebsite.com/test,它仍然会返回到mywebsite.com/test并给出完全相同的输出。

这里有什么问题?

因为web.php不限于域。 因此,每个针对您的 Laravel 应用程序的域都可以访问那里定义的路由。

如果您只希望根域访问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