繁体   English   中英

在Laravel中,View不会返回任何内容

[英]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存储

  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM