Question

I am trying to build a way for a Laravel 4.1 application, which is intended to run multiple domains that essentially do the same thing, to load a different template based on the domain. On top of that I also have to be able to configure multiple environments (dev, qa, stage, live) like you would for any other single-site Laravel setup.

To start off, we already have a Laravel 4.1 instance running and configured for each environment. So that piece of it is good to go. We are using this to replace an old series of websites that was horribly built using an odd mixture of Java and PHP (don't ask, it is a nightmare).

Since every single one of these sites does the exact same thing (they have a homepage, faq, contact us, policy pages, a multi-step application form, and a user portal) we don't see the point in using a duplicate code base and maintaining each one individually (as this is for 5 different looking sites with different names).

So I have been tasked with making a new template for each site that looks identical to the old ones (for now) but in Laravel and running from the same code-base. I am doing that with Bootstrap and Basset. The first template works great with Bootstrap and Basset (to build a custom CSS set for each site from customized LESS files). I am able to get the template test page to load up just fine.

Here is the tricky part. I need to switch which template folder we are pulling our views from based on the domain name (myfirstdomain.com vs myseconddomain.com) while being able to switch environments (dev, qa, stage, and live). The environment is getting set by a current_mode file that is particular to the environment and not checked into the repository. That works great. But we need to be able to detect the domain segment (myfirstdomain, myseconddomain portions of the names) and then use that to set the views sub-folder to use.

I intend to name each template piece the same (header, footer, sidebar, etc). The nav will vary. Other aspects may vary. It would be nice to share a common layout, but at this point I would be happy with just template folder switching.

I spoke with another developer about using namespaces and just detecting and switching views that way. But I am not sure how that would work. I am unsure as to where the best place to detect this would be. I am also not sure the best way to switch/set the template/namespace.

Has anyone tried doing anything like this? Does anyone have any ideas?

Was it helpful?

Solution

I think in this case you don't really need to namespace your views, you can just add to your app/start/global.php something like this:

switch (Request::server('HTTP_HOST')) {
    case 'myfirstdomain.com':
        $viewPath = 'one'
        break;

    case 'myseconddomain.com':
        $viewPath = 'two'
        break;
}

Config::set('view.paths', [__DIR__.'/../views/'.$viewPath]);

And your views would have to be in

app/views/one/*
app/views/two/*

Or just

Config::set('view.paths', [__DIR__.'/../views/'.Request::server('HTTP_HOST')]);

And you would have to add your templates to:

app/views/myfirstdomain.com/*
app/views/myseconddomain.com/*

You can also add some code directly to your app/config/view.php:

'paths' => array(__DIR__.'/../views/'.Request::server('HTTP_HOST')),

As pointed by the OP in comments, you can also, add path locations:

View::addLocation($path);

Connect a namespace to the path:

View::addNamespace('company', $path);. 

And if you want to use a company template, now you just have to do:

return View::make('company::layout.default');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top