Laravel 路由似乎不起作用

Laravel routes don't seem to work

我是 Laravel 的新手。

我已经通过 Oracle VirtualBox 设置了 Fedora 23 服务器,安装了 composer 并创建了一个 Laravel 5.3 项目作为 html 文件夹(为方便起见,因为它只能通过虚拟机的 IP 访问地址)。我还禁用了它的防火墙,因为这仅适用于 testing/learning.

我为存储文件夹授予了 755 权限,只需键入 http://«IP» 即可在浏览器上显示欢迎页面。

我创建了一个 index.blade.php 视图,代码与 welcome.blade.php 相同,但将单词 Laravel 更改为 "Something"。问题来了。

我创建了一个 HomeController.php 控制器,这样我就可以毫无错误地获得 php artisan route:cache,因为如果我在 [= 上返回视图49=].php 文件,它给了我一个错误:

[myuser@webserver]$ php artisan route:cache
Route cache cleared!

[LogicException]
Unable to prepare route [/] for serialization. Uses Closure.

所以我的文件看起来像这样:

routes/web.php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

/*
Route::get('/', function () {
    return view('welcome');
});

Route::get('home', function () {
    return view('index');
});
*/

Route::get('/', 'HomeController@getWelcomePage');
Route::get('home', 'HomeController@homepage');

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class HomeController extends Controller
{
        public function homepage(){
                return view('index');
        }

        public function getWelcomePage(){
                return view('welcome');
        }

}

public/.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

/etc/httpd/conf/httpd.conf(出于长度考虑,我在此处删除了示例中的注释行)

ServerRoot "/etc/httpd"

Listen 80

Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html/public"

<Directory "/var/www">
    AllowOverride none
    # Allow open access:
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on

IncludeOptional conf.d/*.conf

完成所有这些之后,我可以通过输入 http://«IP» 看到欢迎页面,但是如果我输入 http://«IP»/home,我只会看到一个空白页面。浏览器告诉我页面返回了 500 Internal Server Error,但是在 storage/logs/laravel.log 没有任何显示(最后一个日志是由于 php artisan route:cache 由于 web.php 文件中返回的视图而无法执行)

[2017-01-19 13:00:49] local.ERROR: exception 'LogicException' with message 'Unable to prepare
route [/] for serialization. Uses Closure.'
in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:995

对我来说最奇怪的部分是,如果我注释路由文件中的所有条目,我会得到一个 route not found exception for http://«IP»/home,但如果我只输入 http:// «IP» 我仍然受到欢迎 blade.

我相信我的问题很简单,但经过数小时的搜索和 trial/error,我仍然无法解决这个问题。

欢迎任何help/suggestions。

编辑: 我的问题不是关闭错误,我为路由创建了控制器方法。我的问题是服务器只给出欢迎 blade,所有其他服务器都给我一个 500 Internal Server Error

事实证明,如果您有任何路由作为闭包(因为它不会缓存该函数),那么您不能将任何路由缓存与 Laravel 5 一起使用。闭包如下:

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

要么停止缓存您的路线,要么取消所有这些关闭。 this GitHub issue.

中的更多信息

听起来您的日志中的错误不是导致 500 错误的错误。如果您删除该日志并尝试导航到该路由,那么您将不会看到任何错误。我上次 运行 进入这个是我的存储目录没有创建 and/or 它们的权限不正确。确保你的存储目录可被网络服务器写入(我通常设置为 775)并且所有目录都存在。如果他们不这样做,请创建它们:

storage/
|
|-- app/
|   |-- public/
|
|-- framework/
|   |-- cache/
|   |-- sessions/
|   |-- views/
| 
|-- logs/

终于明白了!

阅读 https://woohuiren.me/blog/installing-laravel-5-on-fedora/ 上的评论后,一位显然与我有同样问题的用户说了以下内容:

Seem issue this command solve the blank page (error code 500)

$ cd laravel_project

$ php artisan cache:clear

$ chmod -R 777 storage

$ composer dump-autoload

I set SElinux to permissive, should be ok right?

我的页面显示没有问题。