2017-07-09 64 views
1

我剛開始學習laravel框架,並在這裏有一個奇怪的情況。查看不會在Laravel返回任何內容

都在我的web.php首先我有這樣的代碼:

<?php 
Route::get('/', function() { 
    return view('welcome'); 
}); 

Route::get('users', ['uses' => '[email protected]']); 

在我UsersController.php文件我有這樣的代碼:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

class UsersController extends Controller 
{ 
    public function index(){ 
     return view('users.index'); 
    } 
} 

我有一個文件在目錄views/users名爲index.blade.php具有與默認welcome.blade.php相同的內容。但是,當我嘗試return view('users.index');沒有結果(沒有錯誤,或文本),但當我嘗試return view('welcome');它工作得很好。

目錄和文件確實存在。 我正在使用Macbook Pro。

welcome.blade.php/users/index.blade.php內容(默認歡迎頁面):

<!doctype html> 
<html lang="{{ config('app.locale') }}"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 

     <title>Laravel</title> 

     <!-- Fonts --> 
     <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> 

     <!-- Styles --> 
     <style> 
      html, body { 
       background-color: #fff; 
       color: #636b6f; 
       font-family: 'Raleway', sans-serif; 
       font-weight: 100; 
       height: 100vh; 
       margin: 0; 
      } 

      .full-height { 
       height: 100vh; 
      } 

      .flex-center { 
       align-items: center; 
       display: flex; 
       justify-content: center; 
      } 

      .position-ref { 
       position: relative; 
      } 

      .top-right { 
       position: absolute; 
       right: 10px; 
       top: 18px; 
      } 

      .content { 
       text-align: center; 
      } 

      .title { 
       font-size: 84px; 
      } 

      .links > a { 
       color: #636b6f; 
       padding: 0 25px; 
       font-size: 12px; 
       font-weight: 600; 
       letter-spacing: .1rem; 
       text-decoration: none; 
       text-transform: uppercase; 
      } 

      .m-b-md { 
       margin-bottom: 30px; 
      } 
     </style> 
    </head> 
    <body> 
     <div class="flex-center position-ref full-height"> 
      @if (Route::has('login')) 
       <div class="top-right links"> 
        @if (Auth::check()) 
         <a href="{{ url('/home') }}">Home</a> 
        @else 
         <a href="{{ url('/login') }}">Login</a> 
         <a href="{{ url('/register') }}">Register</a> 
        @endif 
       </div> 
      @endif 

      <div class="content"> 
       <div class="title m-b-md"> 
        Laravel 
       </div> 

       <div class="links"> 
        <a href="https://laravel.com/docs">Documentation</a> 
        <a href="https://laracasts.com">Laracasts</a> 
        <a href="https://laravel-news.com">News</a> 
        <a href="https://forge.laravel.com">Forge</a> 
        <a href="https://github.com/laravel/laravel">GitHub</a> 
       </div> 
      </div> 
     </div> 
    </body> 
</html> 
+0

如果視圖不存在,laravel將拋出一個錯誤。如果您只看到白頁,可能是因爲您的錯誤未顯示。檢查日誌文件是否有錯誤。 '存儲/日誌/ laravel.log'或您的PHP默認日誌文件。嘗試拼寫錯誤的視圖名稱,並檢查結果,你看到一個錯誤? – Hamoud

+0

@Hamoud當我拼錯視圖名稱時,也只有一個白色頁面。 'Storage/logs/laravel.log'(文件不存在)並沒有存儲在我的默認php日誌文件中。 – semkius

+0

然後,肯定會顯示一個不顯示的錯誤。檢查您的.env文件並確保它具有以下值: APP_ENV = local APP_DEBUG = true APP_LOG_LEVEL = debug – Hamoud

回答

1

問題出在文件權限。將所有文件更改爲可讀和可寫,並且可以。

chmod -R 777 /way/to/the/path/* 
+0

這是因爲laravel試圖寫入緩存/視圖和laravel.log,但沒有權限。 – Hamoud

1

如果您laravel版本5.1+

試試這個:

Route::get('users', '[email protected]'); 

,如果它不能幫助你:

你可以一步步測試

步驟1: 替代路線:

Route::get('users', function() { 
    return 'Is OK'; 
}); 

和運行網址:localhost:port/users

如果確定

步驟2

Route::get('users', '[email protected]'); 

和替換UserController的指數方法:

public function index() 
{ 
    return 'Is OK'; 
} 

如果OK轉到第三步

resources/views創建test.blade.php文件的內容:

Is ok 

並更換控制器代碼附:

public function index() 
{ 
    return view('test'); 
} 

這將幫助您找到問題所在

+0

不可以。還是一樣的情況。 – semkius

+0

@semkius我更新了我的答案,希望能幫助您找到您的問題 –

+0

步驟1和2都可以。但第三步不起作用。我試圖修改文件,但仍然 - 只是一個白頁。 – semkius

1

你需要給存儲目錄的權限爲777 打開終端,並參觀laravel目錄並更改權限搭配chmod 777存儲:

在的MacBook,你可以通過在終端輸入以下命令做

0
  1. 在Apache檢查日誌,看看有什麼錯誤: /var/log/apache2/error.log

  2. 爲bootstrap/cache /和/ storage文件夾提供755個perms。 sudo chmod -R 755 storage bootstrap/cache

  3. 生成加密密鑰: php artisan key:generate

  4. 檢查你的/ etc/hosts文件,確保它指向正確的虛擬主機。

  5. 重新啓動Apache: sudo service apache2 restart

相關問題