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) They must remain in sync. A, B and C will naturally be separate repositories.

b) The code is same, but the target market is different. Say gaming and food.

c) My idea is to use 3 different config files, and use environment variables on 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. 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?

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • 1
    You could use the concept of a multi tenant site where you use the same DB but have an extra identifier on the tables that identifies which site they belong to (this is normally used to separate users own data in the same table). – oBo Jul 02 '15 at 11:21
  • No I am not going that far. I can afford separate host, database for this. – Ali Gajani Jul 02 '15 at 11:22
  • Its not really a matter of price, its just an idea to separate the data – oBo Jul 02 '15 at 11:24
  • I don't want such a tight coupling and I don't like that idea anyway. **Scalibility** – Ali Gajani Jul 02 '15 at 11:35
  • If you want to separate the database per website (allowing multiple hostnames per website) while using the same codebase. You might take a look at the [project](http://github.com/hyn-me/multi-tenant) I've been working on. If it's not entirely what you need, you might get some inspiration on how to solve the issue at hand. – Luceos Jul 02 '15 at 12:40
  • never mind, you are using 4.2 – Luceos Jul 02 '15 at 14:20
  • Hey @Luceos, your thing looks interesting, but as you know, I am on 4.2. My requirements are 3 repos, with 2 of them feeding /syncing off a master repository. Can you suggest me some ways I can make multiple config files work properly without any issues, on dev and live. – Ali Gajani Jul 02 '15 at 14:48

1 Answers1

1

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 (e.g. 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:

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:

$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...)

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. 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.

You can definitely still make it even more efficient and the code less error-prone (e.g. overwrite the User::get() with User::where('site_id', Site::thisId())->get() or so).

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? Or everything the same but using id to separate the data on a database level.

Does this help with your question?

Community
  • 1
  • 1
Oliver Adria
  • 1,123
  • 11
  • 23
  • I am interested in the first one. But the real question is, config files, environments and deploying. How to actually do it? I do have an idea, but never tried it. Thanks for your answer. – Ali Gajani Jul 02 '15 at 11:26
  • I've updated the answer to include a small snippet on environment detection from here: http://laravel.com/docs/4.2/configuration#environment-configuration. I hope this will help you further. – Oliver Adria Jul 03 '15 at 07:25