简体   繁体   English

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

[英]View doesn't return anything in Laravel

I just started to learn the laravel framework and have a strange situation here. 我刚刚开始学习laravel框架并且在这里有一个奇怪的情况。

First of all in my web.php I have code like: 首先在我的web.php我有以下代码:

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

Route::get('users', ['uses' => 'UsersController@index']);

In my UsersController.php file I have code like: 在我的UsersController.php文件中,我有以下代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

I have a file named index.blade.php in directory views/users that has the same content as the default welcome.blade.php . 我在目录views/users中有一个名为index.blade.php的文件,其内容与默认的welcome.blade.php相同。 But when I try to return view('users.index'); 但是当我尝试return view('users.index'); there is no result (no error, or text) but when I try to return view('welcome'); 没有结果(没有错误或文本)但是当我尝试return view('welcome'); it works just fine. 它工作得很好。

The directory and the file truly exists. 目录和文件确实存在。 I'm using Macbook Pro. 我正在使用Macbook Pro。

welcome.blade.php and the /users/index.blade.php content (default welcome page): 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>

if your laravel version is 5.1+ 如果您的laravel版本是5.1+

try this: 尝试这个:

Route::get('users', 'UsersController@index');

if it doesn't help you : 如果它没有帮助你:

you could test step by step 你可以一步一步地测试

step 1 : replace route : 第1步 :替换路线:

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

and run url : localhost:port/users 并运行url: localhost:port/users

if Is ok 如果还可以

Step 2 : 第2步

Route::get('users', 'UsersController@index');

and replace UserController index method : 并替换UserController索引方法:

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

if Is ok go to step3 : 如果可以,请转到第3

create test.blade.php file in resources/views with content: 使用内容在resources/views创建test.blade.php文件:

Is ok

and replace controller code with: 并将控制器代码替换为:

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

It will help you to find where is problem 它将帮助您找到问题所在

The problem was with file permissions. 问题出在文件权限上。 Change all the files to readable and writeable and it will be okay. 将所有文件更改为可读和可写,这样就可以了。

On macbook you can do it by entering this command in terminal: 在macbook上,您可以通过在终端中输入以下命令来执行此操作:

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

您需要将存储目录的权限授予777打开终端并访问laravel目录并更改权限chmod 777存储

  1. On apache check the logs, see what's wrong: /var/log/apache2/error.log 在apache上检查日志,看看有什么问题: /var/log/apache2/error.log

  2. Give 755 perms to bootstrap/cache/ and /storage folder. 给bootstrap / cache /和/ storage文件夹提供755 perms。 sudo chmod -R 755 storage bootstrap/cache

  3. Generate encryptions key: php artisan key:generate 生成加密密钥: php artisan key:generate

  4. Check your /etc/hosts file, make sure it's pointing to right virtual host. 检查/ etc / hosts文件,确保它指向正确的虚拟主机。

  5. Restart apache: sudo service apache2 restart 重启apache: sudo service apache2 restart

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

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