【发布时间】:2015-03-31 13:45:53
【问题描述】:
我想从 laravel 中的 url 获取子域。现在用这个
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 并希望每次调用时都将它传递给视图 home.blade.php
【问题讨论】:
标签: php .htaccess laravel routing subdomain