I want to get subdomains from url in laravel. Using this at now
App::before(function($request)
{
if(Request::path() == '/' && !Request::ajax()){
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
if($urlParts[0] == 'fasfin' || $urlParts[1] == 'fasfin' && $urlParts[0] == 'www') {Route::get('/', 'HomeController@index');} //check if url is main site
elseif($urlParts[0] == 'www') { $subdomain = $urlParts[1]; return App::make('SubdomainController')->getIndex($subdomain);} //fix for www.subdomain.mydomain.com
else {$subdomain = $urlParts[0]; return App::make('SubdomainController')->getIndex($subdomain);}
} //get subdomain
});
But this is kind of very bad code, i know. Trick from official docs with {subdomain}.mydomain.com doesn't works. I enabled all subdomains from apache with
Server Alias *.mydomain.com
Update
Changed my code to this
if(!Request::ajax()){
$isSubdomain = false;
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
if($urlParts[0] == 'fasfin' || $urlParts[1] == 'fasfin' && $urlParts[0] == 'www') {Route::get('/', 'HomeController@index');}
elseif($urlParts[0] == 'www') { $subdomain = $urlParts[1]; $isSubdomain = true;}
else {$subdomain = $urlParts[0]; $isSubdomain = true;}
if($isSubdomain)
{
$user = App::make('SubdomainController')->checkIndex($subdomain);
if($user instanceof Exception) return View::make('subdomain.notExist');
$shop = App::make('SubdomainController')->getIndex($subdomain);
if($shop instanceof Exception) return 'Shop was already registered, but doesn't created yet';
}
}
At now new question is
How to pass variable to view every time? In the end i have variable $shop and want to pass it into view home.blade.php every time when it called