N
N
nicolaa2020-10-26 14:31:08
Laravel
nicolaa, 2020-10-26 14:31:08

How to implement a marketplace on laravel?

It is necessary to make a kind of marketplace on laravel with mini sites for each store

As I see it

2 platforms on laravel

1- The main site where all information is uploaded from mini sites
2- Platform for mini sites

For each mini site, use its own database, or rather its own prefix in the database
(Each site has its own categories, dynamic pages, products, settings, roughly speaking 4 tables (so far) in the database for each mini site) and its own .env file

It turns out that you need to implement a connection to different databases for one project

How it works now:

In the file `/bootstrap/app.php`

Added code

if (isset($_SERVER['HTTP_HOST'])) {
        $host = idn_to_utf8($_SERVER['HTTP_HOST'], 0, INTL_IDNA_VARIANT_UTS46);
        $envFile = sprintf('site/%s', $host);
        if ($host && file_exists(sprintf('%s/%s', $app['path.base'], $envFile))) {
            $app->loadEnvironmentFrom($envFile);
        }
    }


When entering any domain (attached to this project/server), we look for a file with the name of the domain in the `site` folder (it stores the settings for connecting to the database) and, if it exists, load it instead of the default `.env` file.

Then we check in the routes , connected to the database or not

In `routes/web.php`

try {
        DB::connection()->getPdo();
        if(DB::connection()->getDatabaseName()){
        }
    } catch (\Exception $e) {
        abort(404);
    }


And then in the same file we get the settings for this site (Name, phone number ..) and cache them

$domain = $_SERVER['HTTP_HOST'] ?? '';
    $site = Cache::remember('setting_domain_'.$domain, 1440, function () {
        $site = \App\Setting::get()->keyBy('name')->toArray();
        return $site;
    });


We enter the received data in the `config` function

config(['siteName' => $site['site_name']['value']]);


In the template we get All this works and it seems like with 10 sites the flight is normal How will this solution behave with 1000+ sites? Maybe someone has come across a similar task and knows how to implement it correctly (with less load)

{{Config::get('siteName')}}



Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sanes, 2020-10-26
@Sanes

Yes, it will be fine. Do you have horizontal scaling?
Donor sites may be distributed across multiple cluster nodes.

A
Alex Wells, 2020-10-26
@Alex_Wells

The solution is very stupid and wrong.
It would be correct to use one database with a normal structure. Tie everything you need (these are your "categories, dynamic pages, products, settings") to the "mini-site" entity and that's it.

M
MVP_Master, 2020-10-26
@MVP_Master

One base, one backend. No additional bases. Exchange with donors via api or parsing.

P
pLavrenov, 2020-10-27
@pLavrenov

Multi-tenancy .
Now you are only thinking about connecting another base. But what about the queues? File systems?
1) leave .env for server settings
2) leave configs for configs, not for data storage
3) Remove logic from routes forever!
4) there is a powerful package https://github.com/tenancy/tenancy/tree/master

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question