【问题标题】:Laravel routes return 500 errorLaravel 路由返回 500 错误
【发布时间】:2017-11-13 23:53:38
【问题描述】:

我将基本的 Laravel 项目部署到服务器。当我点击我的域时,它会返回默认的欢迎视图。当我在代码中添加简单的道路(见下文)并尝试在浏览器中输入该路线时,它会返回 500 内部错误。除“/”根路由外,所有路由都返回 500 错误。

文件夹结构:

/
#laravel
#subdoms
##api

Laravel 文件在 laravel 目录中,但公共目录中的文件在 api 目录中。

api目录下的.htaccess文件:

<IfModule mod_rewrite.c>
  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>

存储目录及其中的所有内容对任何人都是可写、可读、可执行的。

laravel/storage/logs 中没有错误日志。

laravel/routes/web.php:

<?php

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

Route::get('hello', function () { // 500 internal error
    return 'Hello world';
});

服务器信息:

服务器 - Linux CentOS - 阿帕奇 2.2 - 服务器端包括 - SSI - PHP 版本 7.0.17

【问题讨论】:

  • 你好还是/你好?
  • 有关系吗?
  • 是的。 “/”表示路由文件。您在其中调用了welcome.blade.php,因此您想做的进一步调用将在该“/”之后进行。
  • 你在@AshishPatel 的文档中哪里看到的?
  • laravel.com/docs/5.4/routing 使用了大量示例,其中一个是 users/(不在Route::group() 部分),它不使用/users/

标签: php laravel routing


【解决方案1】:

解决方案 1:

在所有路线之前添加“/”;你的情况'/你好'。要么 ;

解决方案 2:

我猜 .htaccess 被忽略了。 http://httpd.apache.org/docs/2.2/en/mod/core.html#allowoverride

*apache2.conf file in /etc/apache2 : *

ServerName localhost

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

选项索引 FollowSymLinks AllowOverride 无 要求所有授予

解决方案 3:

Apache 可能被配置为拒绝 .htaccess 覆盖。在这种情况下,您需要在 VirtualHost 配置中添加一个允许这些的段。有关更多信息,请参阅 Apache 文档。也可能是未启用 mod_rewrite 的情况。如果使用 Ubuntu、Debian 或其他基于 Debian 的操作系统,则 sudo a2enmod rewrite 然后 sudo service apache2 reload 就足够了。

这是我的,它可以工作

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

    RewriteEngine On
    RewriteBase /laravel51/public/
   # change above to your site i.e.,  RewriteBase /whatever/public/

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

【讨论】:

  • 它在本地主机上运行良好。当我把它放在服务器上时,我必须删除: Options -MultiViews 因为这样我什至无法加载索引文件。
  • 检查你的公用文件夹是否有你的 .htaccess。检查您的服务器上是否启用了 mod_rewrite。此外,出于安全原因,您的服务器上可能会禁用 .htaccess。尝试将您的 mod_rewrite 放在 部分。如果您在网站上使用虚拟主机,则可能在多个地方有 AllowOverride。因此,请确保在所有配置上添加 AllowOverride All。
【解决方案2】:

对于那些刚刚读完但仍然没有解决方案的人,可能是您刚刚从您的 gitlab 或 github 帐户上的 repo 中克隆了一个项目,而忘记生成 .env 文件和 APP_KEY init。所以去你的项目目录并打开一个终端并运行cp .env.example .env。这将创建.env 文件。然后运行php artisan key:generate

请注意,在我的特殊情况下,laravel 会为每条路线返回 500 错误。

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 2020-09-11
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2018-04-05
    • 2019-04-20
    相关资源
    最近更新 更多