【问题标题】:Laravel - subdomain routing not working as expectedLaravel - 子域路由无法按预期工作
【发布时间】:2021-10-03 09:49:47
【问题描述】:

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

  • web.php
  • api.php
  • admin.php

我在RouteServiceProviderboot()方法中注册了文件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 并给出确切的相同的输出。

这里有什么问题?

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    因为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"));
    

    【讨论】:

    • 以前怎么没想到呢,一直摆在眼前。尽管如此,这个答案对我来说效果很好,最重要的是一切看起来都很合乎逻辑,没什么奇怪的......
    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    相关资源
    最近更新 更多