Переменная
$_SERVER['HTTP_HOST']
вполне успешно решает эту задачу.
Вот пример моего решения, которое работает без нареканий:
TargetUrlParser.phpnamespace App\Url;
class TargetUrlParser
{
/**
* Get target url
*
* @return string
*/
public static function getUrl()
{
return (string) $_SERVER['HTTP_HOST'];
}
}
HomePageController.phpclass HomePageController extends Controller
{
/**
* @var Site
*/
private $site = null;
/**
* @var SiteSettings
*/
private $siteSettings = null;
/**
* HomePageController constructor.
*/
public function __construct()
{
$this->site = Site::where('http_host', '=', TargetUrlParser::getUrl())->firstOrFail();
$this->siteSettings = (new SiteSettingsFactory($this->site))->createSiteSettings();
}
public function index()
{
return view('pages.index')->with(['settings' => $this->siteSettings, 'site' => $this->site]);
}
}
Миграция для модели Site.php class CreateSitesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sites', function (Blueprint $table) {
$table->increments('id');
$table->string('http_host');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sites');
}
}