簡體   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