Laravel 5 一个实例,多个相同的子域

Laravel 5 one instance, multiple identical subdomains

我想使用一台使用基本 Laravel 安装的服务器,并有引用该安装的子域。所有子域都将像 SaaS 一样。

我环顾四周发现数据库连接很简单,但我想知道您是否可以使用子域的相同代码库智能地做到这一点。

子域世界包括其子域所需的最少文件——也许是 public 索引和 bootstrap?希望不要对所有内容进行符号链接。

我不担心服务器配置,我只是想为 Laravel 代码指出正确的方向,比如处理请求然后指向该子域的中间件?

我读过的很多话题都没有看起来标准的答案,有什么想法或链接吗?

此外,如果它是一个多服务器设置,用一个 NFS 服务器作为核心不是可以吗?

使用 laravel,您可以检查 URL 而无需使用子域,而只需对路由请求进行分组。

Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the domain key on the group attribute array:

Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        // your code
    });
});

在 laravel 文档中阅读更多相关信息 https://laravel.com/docs/5.4/routing#route-group-sub-domain-routing


赏金

您还可以为同一个 Route::group 提供更多参数,例如

Route::group(['domain' => '{subdomain}.{domain}.{tld}'], function () {
    Route::get('user/{id}', function ($account, $id) {
        // your code
    });
});

同时,您可以决定使用 Route::pattern 定义来限制要接受的域参数。

Route::pattern('subdomain', '(dev|www)');
Route::pattern('domain', '(example)');
Route::pattern('tld', '(com|net|org)');
Route::group(['domain' => '{subdomain}.{domain}'], function () {
    Route::get('user/{id}', function ($account, $id) {
        // your code
    });
});

在前面的示例中,将接受并正确路由以下所有域

  • www.example.com
  • www.example.org
  • www.example.net
  • dev.example.com
  • dev.example.org
  • dev.example.net