How can I handle subdomains with one laravel installation

前端 未结 7 1992
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 09:19

I am creating a laravel project for which I need one laravel installation and use its instance in sub-domain with separate database. And th

相关标签:
7条回答
  • 2020-12-29 10:12

    Just stumbled across this question, and IMHO sometimes the simplest suggestion is the easiest.

    I just place a simple switch at the head of the /config/database.php file:

    switch($_SERVER['HTTP_HOST'])
    {
    case 'dev.yoursite.com':
        $selectedDatabase = 'mysite_dev';
        break;
    case 'yoursite.com':
    default:
        $selectedDatabase = 'mysite_live';
        break;
    }
    

    Then simply use the variable within the returned config variable.

    return [
        'connections' => 
            ['mysql' =>
                 ['database' => $selectedDatabase,
                  'username' => 'user_name',
                  'password' => 'xxxxxxxxx',
                 ],
            ]
        ];
    

    I know its not the laravel way, but it'll get you out of a fix if you just want to open up a quick testing environment using the same PHP coding, but a test instance of your database.

    0 讨论(0)
提交回复
热议问题