Php 拉维子域解析
我想从laravel中的url获取子域。现在就用这个Php 拉维子域解析,php,.htaccess,laravel,routing,subdomain,Php,.htaccess,Laravel,Routing,Subdomain,我想从laravel中的url获取子域。现在就用这个 App::before(function($request) { if(Request::path() == '/' && !Request::ajax()){ $urlParts = explode('.', $_SERVER['HTTP_HOST']); if($urlParts[0] == 'fasfin' || $urlParts[1] == 'fasfin' &
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
});
但我知道这是一种非常糟糕的代码。来自官方文档的{subdomain}.mydomain.com的技巧不起作用。我使用启用了apache中的所有子域
Server Alias *.mydomain.com
使现代化
把我的代码改成这个
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';
}
}
现在新的问题是
如何每次都将变量传递给视图?
最后,我有一个变量$shop,每次调用它时都希望将它传递到view home.blade.php中
View::share('shop', $shop);
你试过了吗