Reputation: 2501
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!
Upvotes: 3
Views: 2419
Reputation: 29059
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);
Upvotes: 0
Reputation: 3879
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.
Upvotes: 1