weblbdesigns's avatar

Multiple websites built on one Laravel framework?

Whats the best way to have multiple domains and a cms site built on the same Laravel framework? For example, lets say I have 3 sites. www.site1.com www.site2.com admin.site1 com

site1 and site2 share a database and many functions but need separate domains. The admin.site1.com is a cms for both site1 and site2.

Any approaches out there for building this configuration?

Using Laravel 5 and homestead.

0 likes
7 replies
bashy's avatar

Some things to think about;

  • Direct both domains to the same server/documentroot with Nginx/Apache/DNS.
  • Maybe have a sites table in the database to pick up the domain and check it. That way you can manage that in the backend (I did this a similar way in CodeIgniter a few months back).
  • Direct the subdomain to a namspaced backend folder with controllers only for admin. Protect with filter/middleware
pstephan1187's avatar

I am doing something very similar. Here is how I am doing it:

Routes:

//this is for the main default domain that I host my main site/CMS
Route::group(['domain' => 'maindomainforcms.com'], function(){
    Route::get('/', function(){ return 'this is the index page'; });
});

//this catches the rest of the domains and all their pages:
Route::any('{all}', 'SitesPublicController@index')->where('all', '.*');

SitesPublicController.php

use App\Http\Middleware\VerifiedDomain;

class SitesPublicController extends Controller {

    public function __construct(){
        $this->middleware('domain.verify');
    }

    public function index(VerifiedDomain $verified_domain){
        $Site = $verified_domain->getSite();
        $Page = $Site->getPageByRequest();//This method gets the url segments then selects the proper page

        if(!$Page){ throw new NotFoundHttpException; }

        return $Page->content;
    }

}

VerifiedDomain.php middleware file:

use Closure;
use App\Domain;//this is just a model where I associate domains with sites
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class VerifiedDomain {

    protected $Domain;

    public function handle($request, Closure $next){

        $domain = $_SERVER['SERVER_NAME'];
        $Domain = Domain::where('domain', $domain)->first();

        if(!$Domain){ throw new NotFoundHttpException; }

        $Site = $Domain->site;

        if(!$Site){
            throw new NotFoundHttpException;
        }

        $this->Domain = $Domain;

        return $next($request);
    }

    public function getDomain(){
        return $this->Domain;
    }

    public function getSite(){
        return $this->Domain->site;
    }

}

I also assign the VerifiedDomain Class as a singleton so I can use Dependency Injection on the Controllers. I hope this helps.

EDIT: I left this out: I added this to my http kernel file:


protected $routeMiddleware = [ //other stuff... 'domain.verify' => 'App\Http\Middleware\VerifiedDomain', ];
10 likes
sahanpramod's avatar

@pstephan1187 i have 4 laravel web sites. 1st web site is main web site. I want another web sites(2,3,4) user table and user dashboard connect to main web site. user can login main web site after access (2,3,4) web sites. how did it?

manzanofab's avatar

I need something similar, I will need to still have an installation of Laravel per domain? and even if i do the rerouting, how can i manage that the domain uses the files of the primary directory?

Thanks

jekinney's avatar

Just a random thought throwing out.

Have a laravel app for your data layer, patterns etc (basically everything business needs for all your sites). If you want one admin backend to manages all sites use this app.

Luman app for each site. Import the requirements via psr 4 and namespaces. Couple of controllers, views and routes.

IeD3vil's avatar

I have created a package but have not published yet... i set up each site with its own template, shared error pages (404/500/etc) and shared admin. Each site has its own database, i even created artisan commands to migrate all sites at once... BUT it is not complete yet. In working order, but missing artisan commands, and custom routes and controllers for custom functionality built on top of the main app. Once i am happy with how it works, i will create a package on packagist.

just check my webpage for updates, if you are interested - http://www.ied3vil.com

Please or to participate in this conversation.