[英]View doesn't return anything in Laravel
我剛剛開始學習laravel框架並且在這里有一個奇怪的情況。
首先在我的web.php
我有以下代碼:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('users', ['uses' => 'UsersController@index']);
在我的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>
如果您的laravel版本是5.1+
嘗試這個:
Route::get('users', 'UsersController@index');
如果它沒有幫助你:
你可以一步一步地測試
第1步 :替換路線:
Route::get('users', function () {
return 'Is OK';
});
並運行url: localhost:port/users
如果還可以
第2步 :
Route::get('users', 'UsersController@index');
並替換UserController索引方法:
public function index()
{
return 'Is OK';
}
如果可以,請轉到第3 步 :
使用內容在resources/views
創建test.blade.php文件:
Is ok
並將控制器代碼替換為:
public function index()
{
return view('test');
}
它將幫助您找到問題所在
問題出在文件權限上。 將所有文件更改為可讀和可寫,這樣就可以了。
在macbook上,您可以通過在終端中輸入以下命令來執行此操作:
chmod -R 777 /way/to/the/path/*
您需要將存儲目錄的權限授予777打開終端並訪問laravel目錄並更改權限chmod 777存儲
在apache上檢查日志,看看有什么問題: /var/log/apache2/error.log
給bootstrap / cache /和/ storage文件夾提供755 perms。 sudo chmod -R 755 storage bootstrap/cache
生成加密密鑰: php artisan key:generate
檢查/ etc / hosts文件,確保它指向正確的虛擬主機。
重啟apache: sudo service apache2 restart
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.