简体   繁体   English

使用 Laravel 4.2 进行多站点设置

[英]Multisite setup with Laravel 4.2

I have an interesting problem that the community might want to bask in and help out.我有一个有趣的问题,社区可能想要解决这个问题并提供帮助。 I have a custom forum software.我有一个自定义论坛软件。 Call it A. Now, I intended to roll out different versions of it, call them B and C. But the caveats are the following.称之为 A。现在,我打算推出它的不同版本,称之为 B 和 C。但注意事项如下。

a) They must remain in sync. a) 它们必须保持同步。 A, B and C will naturally be separate repositories. A、B 和 C 自然是单独的存储库。

b) The code is same, but the target market is different. b) 代码相同,但目标市场不同。 Say gaming and food.说游戏和食物。

c) My idea is to use 3 different config files, and use environment variables on Laravel Forge. c) 我的想法是使用 3 个不同的配置文件,并在 Laravel Forge 上使用环境变量。

I am sure someone has a better solution to this.我相信有人对此有更好的解决方案。 I don't know how this will pan out on localhost and production.我不知道这将如何在本地主机和生产中成功。 I need to hear stories of people who have had experiences before, and what is the recommended way to do it.我需要听听以前有过经验的人的故事,以及推荐的方法是什么。

To put into perspective my situation, I am trying to create 3 config files in a folder /config/multi-site and naming them a.php, b.php, c.php.为了正确看待我的情况,我试图在文件夹 /config/multi-site 中创建 3 个配置文件,并将它们命名为 a.php、b.php、c.php。 Beyond that, I feel dumb, because I don't know how to boot these config files when a specific version of the forum is running.除此之外,我觉得自己很愚蠢,因为我不知道在特定版本的论坛运行时如何启动这些配置文件。 Also, how do I sync repo B and C with A using Github?另外,如何使用 Github 将 repo B 和 C 与 A 同步?

I suppose there's no real "perfect" answer, it depends on what you want and where you want to deal with the problems.我想没有真正的“完美”答案,这取决于您想要什么以及您想在哪里处理问题。

Separate Repositories单独的存储库

If you are using separate repositories, make sure that the databases and the migrations AND some of the content (eg specific variables / values that should be available for all the sites) are also synced.如果您使用单独的存储库,请确保数据库和迁移以及某些内容(例如,应可用于所有站点的特定变量/值)也同步。 That's one point that gave me headaches.这是让我头疼的一点。

You would set the environment stuff in files in您将在文件中设置环境内容

app/config/

and then through environment detection pick the right one depending on your site?然后通过环境检测根据您的站点选择正确的一个? http://laravel.com/docs/4.2/configuration#environment-configuration : http://laravel.com/docs/4.2/configuration#environment-configuration

If you need more flexible environment detection, you may pass a Closure to the detectEnvironment method, allowing you to implement environment detection however you wish:如果您需要更灵活的环境检测,您可以将 Closure 传递给 detectEnvironment 方法,允许您根据需要实现环境检测:

 $env = $app->detectEnvironment(function() { return $_SERVER['MY_LARAVEL_ENV']; });

And then based on that you can call the appropriate config file.然后基于此您可以调用适当的配置文件。

Either for each website a different set of files in one repo (local, staging, live) or perhaps all the files in one repo (site1-local, site1-staging, site1-live, site2-local...)对于每个网站,一个存储库(本地、暂存、实时)中的一组不同文件或一个存储库中的所有文件(site1-local、site1-staging、site1-live、site2-local...)

Haven't tested this out yet, though.不过还没有测试过。

One codebase + one database一个代码库+一个数据库

Another option would be (which is what I did in the end) to use the same codebase and have one database and then based on the domain visited, the specific data would be shown.另一种选择是(这是我最后所做的)使用相同的代码库并拥有一个数据库,然后根据访问的域,显示特定数据。 So in many database tables you would have to have "site_id" as a column.因此,在许多数据库表中,您必须将“site_id”作为列。 And when you search for users instead of当你搜索用户而不是

User::where('name', 'Jeffrey')->get();

it would be something like它会是这样的

User::where('site_id', Site::GetThisId())->where('name', 'Jeffrey')->get();

Where GetThisId() checks for the environment / domain and returns the correct id.其中 GetThisId() 检查环境/域并返回正确的 id。

You can definitely still make it even more efficient and the code less error-prone (eg overwrite the User::get() with User::where('site_id', Site::thisId())->get() or so).你仍然可以让它更高效,代码更不容易出错(例如用 User::where('site_id', Site::thisId())->get() 覆盖 User::get() 左右)。

This way has worked well for me, since I only have one codebase and one database and one server and I don't need to make sure all the codebases and databases are synced.这种方式对我来说效果很好,因为我只有一个代码库、一个数据库和一台服务器,而且我不需要确保所有代码库和数据库都同步。 Perhaps some might see as not so elegant, but it has helped to create new sites fast.也许有些人可能认为它不那么优雅,但它有助于快速创建新站点。

The question is where you separate问题是你在哪里分开

So the actual question is where you want to "separate" the sites.所以实际的问题是你想在哪里“分离”站点。 Do you want them on separate repos / servers and everyone has the same set of configs but different content?您是否希望它们在单独的存储库/服务器上并且每个人都拥有相同的配置集但内容不同? Or one repo but different configuration file sets for each site?还是一个 repo 但每个站点的配置文件集不同? Or everything the same but using id to separate the data on a database level.或者一切都一样,但使用 id 在数据库级别分离数据。

Does this help with your question?这对您的问题有帮助吗?

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

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