简体   繁体   English

Laravel路线似乎无效

[英]Laravel routes don't seem to work

I'm new to Laravel. 我是Laravel的新手。

I've set up a Fedora 23 server through Oracle VirtualBox, installed composer and created a Laravel 5.3 project as the html folder (for convenience, since it's accessible by just the virtual machine's IP address). 我已经通过Oracle VirtualBox设置了Fedora 23服务器,安装了作曲家,并创建了一个Laravel 5.3项目作为html文件夹(为方便起见,因为只能通过虚拟机的IP地址对其进行访问)。 I also disabled its firewall, since this is just for testing/learning. 我还禁用了它的防火墙,因为这仅用于测试/学习。

I gave storage folder 755 permissions, and I get the welcome page showing on browser by just typing http://«IP». 我给存储文件夹755授予了权限,只需键入http://«IP»,我就会在浏览器上显示欢迎页面。

I created a index.blade.php view, with the same code as welcome.blade.php but changing the word Laravel to "Something". 我创建了一个index.blade.php视图,其代码与welcome.blade.php相同,但是将Laravel改为“ Something”。 Now comes the problem. 现在出现了问题。

I've created a HomeController.php controller, so that I could get the php artisan route:cache without errors, since if I returned the view on routes/web.php file, it gave me an error: 我创建了一个HomeController.php控制器,这样我就可以毫无错误地获得php artisan route:cache,因为如果我在route / web.php文件上返回视图,它就会给我一个错误:

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

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

So my files look like this: 所以我的文件看起来像这样:

routes/web.php 路线/ 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 应用程序/ HTTP /控制器/ 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 公共/的.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 ^(.*)/$ /$1 [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 (I removed the comment lines in the example here for length purposes) /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

After all this, I can see the welcome page by typing http://«IP», but if I type http://«IP»/home I only get a blank page. 完成所有这些之后,我可以通过键入http://«IP»来看到欢迎页面,但是如果键入http://«IP»/ home,我只会得到一个空白页面。 Browser tells me that the page returned with a 500 Internal Server Error, however at storage/logs/laravel.log nothing shows up (the last log was the error due to the php artisan route:cache not being able to execute due to the view being returned in the web.php file) 浏览器告诉我该页面返回了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

The weirdest part for me is that if I comment ALL the entries in the route file, I get a route not found exception for http://«IP»/home, but if I just type http://«IP» I still get the welcome blade. 对我来说,最奇怪的部分是,如果我注释了路由文件中的所有条目,则会发现http://«IP»/ home的路由未找到异常,但是如果我只输入http://«IP»,我仍然得到欢迎刀片。

I believe that my problem is something really simple, but after hours of searching and trial/error, I still can't solve this. 我相信我的问题确实很简单,但是经过数小时的搜索和反复试验,我仍然无法解决此问题。

Any help/suggestions are welcome. 欢迎任何帮助/建议。

EDIT: My problem is not the Closure error, I created controller methods for the routes. 编辑:我的问题不是关闭错误,我为路由创建控制器方法。 My problem is that the server only gives the welcome blade, all others give me an 500 Internal Server Error 我的问题是服务器只给了欢迎刀片,其他所有给了我500 Internal Server Error

Finally got it! 终于明白了!

After reading comments on https://woohuiren.me/blog/installing-laravel-5-on-fedora/ , an user that apparently had the same issue as me said the following: 在阅读了https://woohuiren.me/blog/installing-laravel-5-on-fedora/上的评论后,一个显然与我有相同问题的用户说:

Seem issue this command solve the blank page (error code 500) 似乎此命令解决了空白页问题(错误代码500)

$ cd laravel_project $ cd laravel_project

$ php artisan cache:clear $ php artisan cache:clear

$ chmod -R 777 storage $ chmod -R 777存储

$ composer dump-autoload $ composer dump-autoload

I set SElinux to permissive, should be ok right? 我将SElinux设置为宽松,应该可以吗?

I got the page showing with no problem. 我得到的页面显示没有问题。

It turns out you can't use any route caching with Laravel 5, if you have any routes as closures (because it won't cache the function). 事实证明,如果您有任何路由作为闭包,则无法在Laravel 5中使用任何路由缓存(因为它不会缓存该函数)。 A closure being like: 闭包是这样的:

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

Either stop caching your routes, or remove all of these closures. 要么停止缓存您的路由,要么删除所有这些闭包。 More information in this GitHub issue . 有关此GitHub问题的更多信息。

Sounds like the error your log isn't the one that is causing the 500 error. 听起来错误是您的日志不是导致500错误的日志。 If you delete that log and try to navigate to that route then you won't see any errors. 如果删除该日志并尝试导航到该路线,则不会看到任何错误。 The last time I ran into this was my storage directories weren't created and/or the permissions on them weren't correct. 我上次遇到此问题是未创建我的存储目录和/或它们的权限不正确。 Make sure your storage directory is writable by the webserver (I usually set to 775) and all directories exist. 确保您的存储目录可由Web服务器写入(我通常将其设置为775),并且所有目录都存在。 Create them if they don't: 如果不创建它们,请创建它们:

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

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

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