【问题标题】:Laravel subdomain parseLaravel 子域解析
【发布时间】: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


【解决方案1】:

找到了

View::share('shop', $shop);

【讨论】:

  • 如果您只想将其与主页一样的一页进行合并,您可以使用作曲家:View::composer('home', function ($view) {view()->share('shop', $shop);});
猜你喜欢
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 2015-05-24
  • 2015-09-24
  • 2012-08-05
相关资源
最近更新 更多