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

问题描述:

I just started to learn the laravel framework and have a strange situation here.

First of all in my web.php I have code like:

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

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

In my UsersController.php file I have code like:

<?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. But when I try to return view('users.index'); there is no result (no error, or text) but when I try to return view('welcome'); it works just fine.

The directory and the file truly exists. I'm using Macbook Pro.

welcome.blade.php and the /users/index.blade.php content (default welcome page):

<!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>

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:

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

if your laravel version is 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 :

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

and run url : localhost:port/users

if Is ok

Step 2:

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

and replace UserController index method :

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

if Is ok go to step3:

create test.blade.php file in resources/views with content:

Is ok

and replace controller code with:

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

It will help you to find where is problem

You Need To Give permission of Storage directory to 777 Open the terminal and visit to laravel directory and change the permission chmod 777 storage

  1. On apache check the logs, see what's wrong: /var/log/apache2/error.log

  2. Give 755 perms to bootstrap/cache/ and /storage folder. sudo chmod -R 755 storage bootstrap/cache

  3. Generate encryptions key: php artisan key:generate

  4. Check your /etc/hosts file, make sure it's pointing to right virtual host.

  5. Restart apache: sudo service apache2 restart