Tijs Proost
Tijs Proost

Reputation: 1

Laravel - Multi Site/Project

I'm currently looking for a framework to support my own new framework. Laravel seems to be my best choice.

I have a very specific structure in mind. My project will have a base design with common models, views and controllers, as well as some sub projects with there specific views. The goal is to provide a platform for owners of the same business, where we provide a unique website. In many cases only the design varies, whilst the structure and components stay the same. My idea was to create a maintainable structure on Laravel which every sites pulls its models, controllers and views from and if there is a specific need an additional view can be created.

Has somebody had experience with a similar project in the past?

I see some major obstacles:

project setup

Upvotes: -2

Views: 1096

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87789

Laravel is very flexible and highly configurable, you should have no problem doing things like that at all. As for the database, for example, you can create two connections: main, a fixed connection to your main database tables, and project for the current project tables, here's what it should look like:

'main' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
],

'project' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
],

And you should be able to:

Configure model connection:

<?php

namespace App;

class Posts extends Model
{
    protected $connection = 'project';
}

Query connections directly:

 DB::connection('project')->table('users')->where('activated', true)->get();

Configure the database in run time:

 config([
     'database.connections.project.database' => 'project1db',
     'database.connections.project.user' => $user, 
     'database.connections.project.password' => $password,
 ]);

As for the views, you can tell Laravel to find views wherever you need by simply doing:

View::addLocation('/path/to/project1/');

Upvotes: 3

Related Questions