简体   繁体   English

多个子域访问的单个 Laravel 实例

[英]Single Instance of Laravel accessed by Multiple subdomains

I am completely no-wise to Laravel.我对 Laravel 完全不明智。 But unfortunately, just a week ago, I got a project build upon Laravel.但不幸的是,就在一周前,我得到了一个基于 Laravel 的项目。 I set that up and running on my hosting machine.我在我的主机上设置并运行它。 It's working good.它运作良好。 But now I need to setup subdomains and access the same instance of Laravel project.但是现在我需要设置子域并访问 Laravel 项目的同一个实例。

I struggled and somehow made it.我挣扎并以某种方式成功了。 Now my subdomains can access main domain laravel instance.现在我的子域可以访问主域 laravel 实例。

Here are complete details:以下是完整的详细信息:

Suppose my main domain with Laravel installation of project is eg example.com .假设我使用 Laravel 安装项目的主域是example.com Then created another subdomain as sub.example.com and point it's DocumentRoot directory to my main domain "example.com"然后创建另一个子域作为sub.example.com并将它的 DocumentRoot 目录指向我的主域“example.com”

Then I created a new .env file with name of my subdomain as .env.sub.example.com in my main Laravel installation and updated following file bootstrape/app.php with following code so it can read .env file depending on the request coming from which domain and serve accordingly.然后我在我的主要 Laravel 安装中创建了一个新的.env文件,我的子域名称为.env.sub.example.com并使用以下代码更新了以下文件bootstrape/app.php以便它可以根据请求读取.env文件来自哪个域并相应地提供服务。

/*
|-----------------------------------------------
| Load domain-specific .env file if it exists
|-----------------------------------------------
*/

if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){

    $domain = $_SERVER['HTTP_HOST'];

    if (isset($domain)) {
        $dotenv = Dotenv\Dotenv::createImmutable(base_path(), '.env.'.$domain);

        try {
            $dotenv->load();
        } catch (\Dotenv\Exception\InvalidPathException $e) {
        // No custom .env file found for this domain
        }
    }
}
// ...

Everything works good but it has problems with sessions and some of my modules installed are conflicting.一切正常,但会话有问题,我安装的一些模块发生冲突。 If someone could point my mistake or tell me what am I missing.如果有人可以指出我的错误或告诉我我错过了什么。

How can I use my single instance of Laravel with Subdomains?如何将 Laravel 的单个实例与子域一起使用?

My routes/web.php file contains Routes like this我的routes/web.php文件包含这样的路由

Route::group(['prefix' => 'settings'], function () {
   Route::post('categories/import', 'Settings\Categories@import')->name('categories.import');
   Route::get('categories/export', 'Settings\Categories@export')->name('categories.export');
   Route::resource('categories', 'Settings\Categories');
});

@Airy, ideally you would want to look into multi-tenancy for your application. @Airy,理想情况下,您希望为您的应用程序研究多租户。 It's easier to maintain and you wouldn't have to update the Laravel internals to get it to work and that is my suggestion.它更易于维护,您不必更新 Laravel 内部结构即可使其正常工作,这是我的建议。

Take a look at some of the Laravel packages that may help you with this:看看一些 Laravel 包可以帮助你解决这个问题:

  1. https://tenancyforlaravel.com/ https://tenancyforlaravel.com/
  2. https://spatie.be/docs/laravel-multitenancy/v2/introduction https://spatie.be/docs/laravel-multitenancy/v2/introduction
  3. https://tenancy.dev/ https://tenancy.dev/

Hopefully the above helps.希望以上内容有所帮助。

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

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