2017-07-18 49 views
1

我學會了laravel使用version 5.4 6個月。然後我安裝了最新的xampp。但是現在我得到了一個建在laravel 5.1上的項目。但是,當我想運行應用程序,它給了我錯誤(未定義的索引:http_host)!我怎樣才能找到錯誤來自哪裏? 我該如何解決這個問題?我在網上搜索,但沒有發現富有成效。 你能幫我嗎? 存儲/ log.php:Laravel 5.1在運行'php artisan serve'時給出'未定義索引:http_host'

[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']))); 
+0

後您的日誌,而不是自動加載,請 – vietnguyen09

+0

我在哪裏可以找到日誌文件? – Fawel

+0

你可以在'storage \ logs'中找到,刪除這裏的每個文件並重新運行你的artisan命令以獲取錯誤,並打開你的日誌文件並更新你的文章 – vietnguyen09

回答

2

可變$_SERVER['HTTP_HOST']僅WO RK上的瀏覽器,而不是PHP-CLI,所以當你運行命令php artisan$_SERVER['HTTP_HOST']將不存在,您可以點擊這裏Undefined index HTTP_HOST even though it is checked

在這種情況下,你可以通過檢查$_SERVER['HTTP_HOST']存在修復您的代碼。

關於線條將解決您的問題,並有一點改進。這是固定代碼的完整版本並經過測試。

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); 
     } 
    } 
} 

希望這有助於,

+0

謝謝作品!你能解釋一下代碼的作用嗎?正如我所說,這不是我的任務,但我會努力。我有'site_infos'有'site_alias'列的表。我知道每次頁面加載時都會運行引導方法。那麼,啓動方法中的代碼是幹什麼的? – Fawel

+0

你可以從這裏閱讀https://laravel.com/docs/5.4/providers,但無論如何,我認爲你應該找到另一種方式來做到這一點,而不是發送查詢到您的數據庫的每一個請求,這是不好的。 – vietnguyen09

+0

我該怎麼做而不是那個? – Fawel