4

I want to set up multi domains in my laravel 5.1.

I have 3 different domains

www.domain1.com
www.domain2.com
www.domain3.com

every domain will have some common functionality and some different functionality but with different themes

i want my view structure like that

resource/www.domain1.com
resource/www.domain2.com
resource/www.domain3.com

i am unable to separate views how can i achieve this?

**

2
  • plz clarify what you want to achieve Commented Nov 29, 2015 at 9:33
  • i want to handle many websites in one laravel 5.1 code. View of each website will be different. Commented Nov 29, 2015 at 9:42

3 Answers 3

6

You should create middle ware to modify path to view folder:

namespace App\Http\Middleware;

use Closure;
use Illuminate\View\FileViewFinder;
use Illuminate\Support\Facades\View;

class ModifyViewFolder 
{

  public function handle($request, Closure $next)
  {
      $finder = new FileViewFinder(app()['files'], [
        app_path('../resources/views/' . $request->server->get('HTTP_HOST')),
        app_path('../resources/views/'),

      ]);
      View::setFinder($finder);

      return $next($request);
  }

}

Then register it in Your Kernel.php. This will fallback to default folder if view in domain specific folder does not exists.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to get some views from the shared views, and others from a domain specific folder? Maybe by creating a custom FileViewFinder?
@Rudie You are passing array to FileViewFinder, so You can pass as many values as You want. See updated answer.
what about separate databases how i handle tha?t
@bogdan please guide me how i separate databases for each application
1

You can effortlessly do this by using this package Gecce Multi Domain

Installation Add gecche/laravel-multidomain as a requirement to composer.json:

{
    "require": {
        "gecche/laravel-multidomain": "4.*"
    }
}

Update your packages with composer update or install with composer install.

You can also add the package using composer require gecche/laravel-multidomain and later specify the version you want (for now, dev-v1.1.* is your best bet).

This package needs to override the detection of the HTTP domain in a minimal set of Laravel core functions at the very start of the bootstrap process in order to get the specific environment file. So this package needs a few more configuration steps than most Laravel packages.

Installation steps:

replace the whole Laravel container by modifying the following lines at the very top of the bootstrap/app.php file.

//$app = new Illuminate\Foundation\Application(
$app = new Gecche\Multidomain\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

update the two application Kernels (HTTP and CLI). At the very top of the app/Http/Kernel.php file , do the following change:

//use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Gecche\Multidomain\Foundation\Http\Kernel as HttpKernel;

Similarly in the app/Console/Kernel.php file:

//use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Gecche\Multidomain\Foundation\Console\Kernel as ConsoleKernel;

Override the QueueServiceProvider with the extended one in the $providers array in the config/app.php file: //Illuminate\Queue\QueueServiceProvider::class, Gecche\Multidomain\Queue\QueueServiceProvider::class, publish the config file.

php artisan vendor:publish 

(This package makes use of the discovery feature.)

Following the above steps, your application will be aware of the HTTP domain in which is running, both for HTTP and CLI requests, including queue support.

Usage This package adds three commands to manage your application HTTP domains:

domain.add artisan command

The main command is the domain:add command which takes as argument the name of the HTTP domain to add to the application. Let us suppose we have two domains, site1.com and site2.com, sharing the same code.

We simply do:

php artisan domain:add site1.com 

and

php artisan domain:add site2.com 

These commands create two new environment files, .env.site1.com and .env.site2.com, in which you can put the specific configuration for each site (e.g. databases configuration, cache configuration and other configurations, as usually found in an environment file).

The command also adds an entry in the domains key in config/domains.php file.

In addition, two new folders are created, storage/site1_com/ and storage/site2_com/. They have the same folder structure as the main storage.

Customizations to this storage substructure must be matched by values in the config/domain.php file.

domain.remove artisan command The domain:remove command removes the specified HTTP domain from the application by deleting its environment file. E.g.:

php artisan domain:remove site2.com 

Comments

0

I understand this post is rather old, but as with all things Laravel, there is more than one way to do anything. I was able to achieve this effect by overriding the ViewServiceProvider as such. However, this solution will apply to the entire site as the accepted answer will allow you to apply the templating to web groups.

class

 TemplateServiceProvider extends ViewServiceProvider
{
    /**
     * Register the view finder implementation.
     *
     * @return void
     */
    public function registerViewFinder()
    {
        $this->app->bind('view.finder', function ($app) {
            $paths = [];

            /*
             *  Pull our template from our site name
             */
            $template = Template::where('domain', Request::server('SERVER_NAME'))->first();
            if($template)
            {
                $paths = [
                    $app['config']['view.paths.templates'] . DIRECTORY_SEPARATOR . $template->tag
                ];
            }


            /*
             *  Default view path is ALWAYS last
             */
            $paths[] = $app['config']['view.paths.default'];

            return new FileViewFinder($app['files'], $paths);
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.