简体   繁体   English

在AppFog上运行的Slim Framework的路由问题

[英]Routing issues with Slim Framework running on AppFog

I've been successfully running Slim apps on a couple different servers and tried setting one up on AppFog today using the same structure, but it isn't running normally. 我已经成功地在几个不同的服务器上运行了Slim应用程序,并尝试使用相同的结构在AppFog上设置一个,但运行不正常。

I'll start with my directory structure: 我将从目录结构开始:

.htaccess
/public
    .htaccess
    index.php
    /routes
/Slim

The root .htaccess file contains the DocumentRoot code from the AppFog docs. 根.htaccess文件包含AppFog文档中的DocumentRoot代码。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^brs.aws.af.cm$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.brs.aws.af.cm$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

The /public directory is where my api code will go, and the Slim index.php and .htaccess files currently are. / public目录是我的api代码所在的目录,当前是Slim index.php.htaccess文件。 The index.php file contains two simple routes: index.php文件包含两个简单的路由:

require '../Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();


// Default GET route
$app->get('/', function () {
    echo "Default GET route";
});


// Hello World route
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});


$app->run();

The server is setup at http://brs.aws.af.cm/ and I've listed the main routes below: 服务器设置为http://brs.aws.af.cm/ ,我在下面列出了主要路线:

  1. / => uses the default GET route / =>使用默认的GET路由
  2. /hello/john => 404 Error / hello / john => 404错误
  3. /public/hello/john => works, but requires "/public" in the url / public / hello / john =>可以,但是在URL中需要“ / public”

And here's some extra weirdness. 还有一些额外的怪异。 Seven-character routes result in a 404 error, six or less end up using the default GET route. 七个字符的路由会导致404错误,使用默认的GET路由会导致六个或更少的错误。

  1. /123456 => shouldn't work, but uses the default GET route / 123456 =>不起作用,但使用默认的GET路由
  2. /1234567 => 404 error / 1234567 => 404错误

I'm completely stumped. 我完全迷住了。 I figure it has something to do with the DocumentRoot code, but I'm not sure what exactly. 我认为这与DocumentRoot代码有关,但是我不确定到底是什么。 I've also tried setting 我也尝试过设定

RewriteBase /public/

in /public/.htaccess but it doesn't seem to affect anything. /public/.htaccess中,但似乎没有任何影响。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

There is a bug in the Slim PHP framework in Environment.php line 143. In particular, it assumes that the $_SERVER['SCRIPT_NAME'] path is compatible with $_SERVER['REQUEST_URI'] variable. Slim PHP框架的Environment.php第143行中有一个错误。尤其是,它假定$_SERVER['SCRIPT_NAME']路径与$_SERVER['REQUEST_URI']变量兼容。 In most cases this is probably true, however not when using MOD_REWRITE to hide an intermediate directory (as is happening in the .htaccess you quoted). 在大多数情况下,这可能是正确的,但是在使用MOD_REWRITE隐藏中间目录时(例如在您引用的.htaccess中发生的情况),情况并非如此。

What's happening is $_SERVER['SCRIPT_NAME'] looks something like "/public/something..." but (because it is hidden), $_SERVER['REQUEST_URI'] looks like "/something...". 发生的是$_SERVER['SCRIPT_NAME']看起来像“ / public / something ...”,但是(因为它是隐藏的), $_SERVER['REQUEST_URI']看起来像是“ / something ...”。

Slim is assuming the request URI is based on the script name, which is not the case here. Slim假设请求URI基于脚本名称,在此情况并非如此。 I plan on notifying the Slim authors of the bug, but wanted to make note of it here as well. 我计划通知Slim作者该错误,但也希望在此进行说明。

You can fix/work around this by modifying Slim/Environment.php line 143 to this: 您可以通过将Slim / Environment.php行143修改为此来解决/解决此问题:

if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {
    $env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite
    $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
} else {
    $env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); //With URL rewrite
    $env['PATH_INFO'] = $_SERVER['REQUEST_URI'];
}
// $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));

At least that seems to work fine in my case. 至少对于我来说,这似乎很好。 I believe the intention was to remove the path from the request uri, but that seems a pretty horrible way of doing it. 我相信这样做的目的是从请求uri中删除路径,但这似乎是一种非常可怕的方式。 If you need subdirectories to keep working you may need to do a bit more thinking. 如果您需要子目录来保持正常工作,则可能需要多做一些思考。 :) :)

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

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