【问题标题】:Lumen simple route request doesn't work流明简单路由请求不起作用
【发布时间】:2015-11-23 17:16:47
【问题描述】:

我在我的网络服务器上安装了 Lumen,但我遇到了路由问题

// http://12.345.678.910/
$app->get('/', function() use ($app) {
    return "This works";
});

但在第二种情况下,他找不到目录

// http://12.345.678.910/api
$app->get('/api', function() use ($app) {
    return "This dont work";
});

在第二种情况下,我收到标准 404 错误。

The requested URL /api was not found on this server.

我使用 Apache、Ubuntu、PHP 5.5 和 Lumen

【问题讨论】:

  • 你启用了Apache url重写模块吗?
  • /index.php/api 有效吗?如果是这样,那么您的 URL 重写不起作用。

标签: laravel lumen


【解决方案1】:

听起来您的 URL 重写不起作用。如果在/api 之前的 URL 中添加index.php 是否有效?

例如,yourdomain.com/api 将变为 yourdomain.com/index.php/api,如果第二个 URL 有效,则重写无效。

如果您的重写不起作用,但您的public 目录中有.htaccess 文件,那么您可能需要在您的Apache 配置中允许覆盖。这是 Ubuntu 上 Lumen 的示例虚拟主机配置。

我已经标记了您需要更改的行。将第一个和第三个更改为指向您网站目录中的public 目录。然后将第二行更改为您在网站上使用的域名。

<VirtualHost *:80>
    DocumentRoot "/var/www/lumen/public"      # Change this line
    ServerName yourdomain.com                 # Change this line
    <Directory "/var/www/lumen/public">       # Change this line
        AllowOverride All    # This line enables .htaccess files
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

您需要重新启动 Apache 才能使这些设置生效。

更好的方法

启用.htaccess 文件应该可以工作,但使用.htaccess 会降低您的网站速度。最好的解决办法是将.htaccess文件的内容放到你的虚拟主机中,然后禁用.htaccess文件。

示例虚拟主机配置如下所示:

<VirtualHost *:80>
    DocumentRoot "/var/www/lumen/public"  # Change this line
    ServerName yourdomain.com             # Change this line

    <Directory "/var/www/lumen/public">   # Change this line
        # Ignore the .htaccess file in this directory
        AllowOverride None

        # Make pretty URLs
        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews
            </IfModule>

            RewriteEngine On

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

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

再一次,您需要重新启动 Apache 才能使这些设置生效。

【讨论】:

    【解决方案2】:

    在您的应用程序的根目录中,创建一个.htaccess 文件(如果它还没有的话)。然后粘贴以下代码:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    我假设您正在使用apache 服务器并打开了mod_rewrite

    阅读 Lumen documentation 中的基本配置部分。

    如果您不确定如何打开 mod_rewrite,此 stackoverflow post 可能会对您有所帮助。

    【讨论】:

      【解决方案3】:

      public 文件夹下的 .htaccess 文件可能丢失。 检查这个:https://github.com/laravel/lumen/blob/master/public/.htaccess

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-03
        • 2015-06-23
        • 1970-01-01
        • 2016-03-26
        • 2021-03-31
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多