laravel 流明简单的路由请求不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32281294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:16:17  来源:igfitidea点击:

Lumen simple route request doesn't work

laravellumen

提问by Ivan Vulovi?

I installed Lumen on my web server, but I have problems with routes

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

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

But in this second case he cant find directory

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

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

In second case I am getting standard 404 error.

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

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

I use Apache, Ubuntu, PHP 5.5 and Lumen

我使用 Apache、Ubuntu、PHP 5.5 和 Lumen

回答by BrokenBinary

It sounds like your URL rewriting isn't working. If you add index.phpto the URL right before the /apidoes it work?

听起来您的 URL 重写不起作用。如果您index.php在 URL 之前添加/api它会起作用吗?

For example, yourdomain.com/apiwould become yourdomain.com/index.php/apiand if the second URL works, then rewriting isn't working.

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

If your rewriting isn't working, but you have the .htaccessfile in your publicdirectory, then you probably need to allow overrides in your Apache configuration. Here is an example virtual host configuration for Lumen on Ubuntu.

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

I've marked the lines you need to change. Change the first and third to point to the publicdirectory in your website's directory. Then change the second line to the domain name you're using with your website.

我已经标记了您需要更改的行。将第一个和第三个更改为指向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>

You'll need to restart Apache for these settings to take effect.

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

A Better Way

更好的方式

Enabling the .htaccessfile should work, but using .htaccessslows down your site some. The best solution is to put the contents of the .htaccessfile in your virtual host, and then disable .htaccessfiles.

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

The example virtual host configuration for that looks like this:

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

<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 ^(.*)/$ / [L,R=301]

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

Once again, you'll need to restart Apache for these settings to take effect.

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

回答by Subash

In the root of your application, create a .htaccessfile if its not already there. Then paste the following code:

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

Options +FollowSymLinks
RewriteEngine On

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

I assuming that you are using apacheserver and have mod_rewriteturned on.

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

Read basic configuration section in Lumen documentation.

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

If you are unsure how to turn on mod_rewrite, this stackoverflow postmight help you.

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

回答by llioor

Probably the .htaccess file under publicfolder is missing. Check this: https://github.com/laravel/lumen/blob/master/public/.htaccess

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