Laravel 5.1在运行'php artisan serve'时给出'undefined index:http_host'

时间:2017-07-18 01:46:07

标签: php laravel-5

我使用laravel了解version 5.4 6个月。我安装了最新的xampp。但现在我开始研究基于laravel 5.1的项目。但是当我想运行应用程序时,它会给我错误(未定义的索引:http_host)!我怎样才能找到错误的来源? 我该如何解决这个问题?我在网上搜索但没有任何结果。 你能帮我吗? 存储/ log.php:

[2017-07-18 21:55:50] local.ERROR: ErrorException: Undefined index: HTTP_HOST in H:\Current\school\school\app\Providers\AppServiceProvider.php:40

堆栈追踪:

#0 H:\Current\school\school\app\Providers\AppServiceProvider.php(40): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', 'H:\\Current\\scho...', 40, Array)
#1 [internal function]: Erp\Providers\AppServiceProvider->boot()
#2 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Container\Container.php(507): call_user_func_array(Array, Array)
#3 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(734): Illuminate\Container\Container->call(Array)
#4 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(717): Illuminate\Foundation\Application->bootProvider(Object(Erp\Providers\AppServiceProvider))
#5 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(Erp\Providers\AppServiceProvider), 18)
#6 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(718): array_walk(Array, Object(Closure))
#7 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php(17): Illuminate\Foundation\Application->boot()
#8 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(203): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#9 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(208): Illuminate\Foundation\Application->bootstrapWith(Array)
#10 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(105): Illuminate\Foundation\Console\Kernel->bootstrap()
#11 H:\Current\school\school\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 {main}  

AppServiceProvider:

public function boot()

{

    if(!Session::get(SITE_ID)){
        $subdomain_name = array_shift((explode(".",$_SERVER['HTTP_HOST'])));
        if(isset($subdomain_name) && !empty($subdomain_name)){
            $domain = $subdomain_name;
        }else{
            $domain = "school";
        }
        $siteToRecollect = DB::table('site_infos')->where('site_alias',$domain)->first();

        if(isset($siteToRecollect->id) && !empty($siteToRecollect->id) && $siteToRecollect->id != 0){

            Session::put(SITE_ID,$siteToRecollect->id);

        }else{

            Session::put(SITE_ID,1);

        }

    }

}

40行:

$subdomain_name = array_shift((explode(".",$_SERVER['HTTP_HOST'])));

1 个答案:

答案 0 :(得分:3)

变量$_SERVER['HTTP_HOST']仅适用于浏览器,而不适用于PHP-CLI,因此当您运行命令php artisan $_SERVER['HTTP_HOST']将不存在时,您可以在此处查看Undefined index HTTP_HOST even though it is checked

在这种情况下,您可以通过检查$_SERVER['HTTP_HOST']是否存在来修复您的代码。

if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
    $gethost = explode(".",$_SERVER['HTTP_HOST']);
    $domain = array_shift($gethost);
}else{
    $domain = "school";
}

关于线路将解决您的问题,并有一些改进。这是固定代码的完整版本并经过测试。

public function boot()
{
    if(!Session::get(SITE_ID)){
        $domain = "school";
        if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
            $gethost = explode(".",$_SERVER['HTTP_HOST']);
            $domain = array_shift($gethost);
        }
        $siteToRecollect = DB::table('site_infos')->where('site_alias', $domain)->first();
        if(isset($siteToRecollect->id) != 0 && !empty($siteToRecollect->id))
        {
            Session::put(SITE_ID, $siteToRecollect->id);
        }else{
            Session::put(SITE_ID,1);
        }
    }
}

希望得到这个帮助,