3

I'm building an application that has multiple domain names linked to it and different front-end views/websites based on and linked to those domain names.

Now I would like to set some variables based on the domain name and make them usable in my controllers and application logic.

for example, all views for the different front-ends are stored in different folders based on the domain name (ziv, dbg, dbe). So let's say, if a user reaches the application via example.com a variable must be set so that the views loaded will be from the folder "exm". It would look like this:

View::make('frontend.' . $folderVariable . '.home')->with('info', $info);

My question is: where should I place such code?

Should it be in the bootstrap file, or in a base controller that all other controllers will inherit? I do need the information on the domain name throughout the whole application, so it needs to be loaded up front.

Thanks in advance!

2
  • i had place it into the base controller in my project.. but not sure if this is the best way to do it. Commented Jan 23, 2015 at 11:44
  • 1
    In Laravel 4.2 you can do that in App:before() located at /app/filters.php. Use View::share() to have it available throughout the views or Config::set() to have it available everywhere. Commented Jan 23, 2015 at 14:51

2 Answers 2

1

Consider using a Service class to handle the current domain and return an appropriate string to use with the View::make() method.

Either that or extend the View class \Illuminate\Support\Facades\View to override the View::make() or to create another method that inserts the relevant string automatically. Also optionally utilising a service provider.

Example of the service class - it doesn't need a service provider (depends on the implementation)

class DomainResolver
{
    private $subdomains;

    public function __construct()
    {
        //Contains sub domain mappings
        $this->subdomains = array(
            'accounts' => 'ziv',
            'billing' => 'exm'
            //Etc etc
        );
    }

    public function getView($view)
    {
        // Should return the current domain/subdomain
        // Replace if I'm wrong (untested)
        $subdomain = \Route::getCurrentRoute->domain();

        if(isset($this->subdomains[$subdomain]))
        {
            return View::make($this->subdomains[$subdomain].'.'$view);
        }
        throw new \Exception('Invalid domain');
    }
}

You would then insert this class where you needed to have a domain specific function performed. I.e - BaseController, View functionality extensions (you could make View::domainMake() that would just call the service class with the value given.

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

7 Comments

So in the ServiceProvider boot method I would put some logic that checks the domain and sets some vars? How would I make that accessible in my controllers for example?
It's a nice solution but it's just for the views. I also want to be able to load different API keys from my config files based on the domain name. Therefore I would like the domain variables to be accessible throughout the application and not just for the views. Do you think that putting this logic in a public function in the base controller and loading that method in the base controllers constructor is better for the functionality I would like? Or isn't this the best solution?
Extending the controller is an option but what if you need that functionality in a different part of your application? Usage of a service class means you can place that class any where in your application and reuse it whilst also making it easily testable. You could then use that service class in an extendable controller AND in the view extension method. If you haven't used service classes before then look in to it, there are many tutorials available.
So you would suggest creating a service provider for checking the logic on the domain name and setting the variables in there? How would I acces the variables then?
I wouldn't condemn it to just "Support". I use them extensively to develop. And you are correct. I should have mentioned dependency injection from the start.
|
0

You could create a middleware like this:

<?php

namespace App\Http\Middleware;

use Closure;

class DetectDomainMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if($_SERVER['SERVER_NAME'] == 'example.com')
        {
          define('domain', 'exm');
        }
        elseif($_SERVER['SERVER_NAME'] == 'example-B.com')
        {
          define('domain', 'B');
        }


        return $next($request);
    }
}

and register this middleware to the kernel.php as global, so it will be executed on each HTTP request.

Now in every file (Controller / View / etc.) you can check on which domain you are

<?php

  if(domain == 'exn') {..}
  if(domain == 'B') {..}

Your view command could be changed to

View::make('frontend.' . domain . '.home')->with('info', $info);

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.