Lumen 简单路由请求不起作用
Lumen simple route request doesn't work
我在我的网络服务器上安装了 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
在应用程序的根目录中,创建一个 .htaccess
文件(如果尚不存在)。然后粘贴以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
我假设您正在使用 apache
服务器并已打开 mod_rewrite
。
阅读 Lumen 中的基本配置部分 documentation。
如果您不确定如何打开 mod_rewrite
,这个 Whosebug post 可能会对您有所帮助。
听起来您的 URL 重写不起作用。如果您在 /api
之前将 index.php
添加到 URL 是否有效?
例如,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 ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
再次强调,您需要重新启动 Apache 才能使这些设置生效。
可能 public
文件夹下的 .htaccess 文件丢失了。
检查这个:https://github.com/laravel/lumen/blob/master/public/.htaccess
我在我的网络服务器上安装了 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
在应用程序的根目录中,创建一个 .htaccess
文件(如果尚不存在)。然后粘贴以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
我假设您正在使用 apache
服务器并已打开 mod_rewrite
。
阅读 Lumen 中的基本配置部分 documentation。
如果您不确定如何打开 mod_rewrite
,这个 Whosebug post 可能会对您有所帮助。
听起来您的 URL 重写不起作用。如果您在 /api
之前将 index.php
添加到 URL 是否有效?
例如,yourdomain.com/api
将变为 yourdomain.com/index.php/api
,如果第二个 URL 有效,则重写无效。
如果您的重写不起作用,但您的 public
目录中有 .htaccess
文件,那么您可能需要在 Apache 配置中允许覆盖。这是 Ubuntu.
我已经标记了您需要更改的行。将第一个和第三个更改为指向您网站目录中的 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 ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
再次强调,您需要重新启动 Apache 才能使这些设置生效。
可能 public
文件夹下的 .htaccess 文件丢失了。
检查这个:https://github.com/laravel/lumen/blob/master/public/.htaccess