- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 fatfree 的路线,发现它的行为异常。
这是我在 index.php 中的代码:
$f3 = require_once(dirname(dirname(__FILE__)). '/lib/base.php');
$f3 = \Base::instance();
echo 'received uri: '.$_SERVER['REQUEST_URI'].'<br>';
$f3->route('GET /brew/@count',
function($f3,$params) {
echo $params['count'].' bottles of beer on the wall.';
}
);
$f3->run();
这是我访问的 URL:http://xx.xx.xx.xx:8090/brew/12
我收到 404 错误:
received uri: /brew/12
Not Found
HTTP 404 (GET /12)
奇怪的是 F3 中的 URI 现在是“/12”而不是“/brew/12”,我想这就是问题所在。
当我检查 base.php (3.6.5) 时,$this->hive['BASE'] = "/brew"和 $this->hive['PATH'] = "/12"。但是如果F3只用$this->hive['PATH']去匹配预定义的路由,是匹配不到的。
如果我将路线更改为:
$f3->route('GET /brew',
并使用 URL:http://xx.xx.xx.xx:8090/brew ,然后路由匹配没有问题。在这种情况下,$this->hive['BASE'] = ""和 $this->hive['PATH'] = "/brew"。如果 F3 将 $this->hive['PATH'] 与预定义路由进行比较,则它们相互匹配。
顺便说一句,我正在使用 PHP 的内置 Web 服务器,并且由于 $_SERVER['REQUEST_URI'](由 base.php 使用)返回正确的 URI,我认为 URL 没有任何问题在我的 .htrouter.php 中重写。
有什么想法吗?我在这里错过了什么?
在这里添加.htrouter.php的内容
<?php
#get the relative URL
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
#if request to a real file (such as a html, image, js, css) then leave it as it is
if ($uri !== '/' && file_exists(__DIR__ . $uri)) {
return false;
}
#if request virtual URL then pass it to the bootstrap file - index.php
$_GET['_url'] = $_SERVER['REQUEST_URI'];
require_once __DIR__ . './public/index.php';
最佳答案
您的问题与您使用 PHP 内置网络服务器的方式直接相关。
如 PHP docs 中所述,这是服务器处理请求的方式:
URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, the lookup for index.php and index.html will be continued in the parent directory and so on until one is found or the document root has been reached. If an index.php or index.html is found, it is returned and $_SERVER['PATH_INFO'] is set to the trailing part of the URI. Otherwise a 404 response code is returned.
If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.
这意味着,默认情况下(没有路由脚本),Web 服务器可以很好地将不存在的 URI 路由到您的文档根目录 index.php
文件。
换句话说,如果你的文件结构是这样的:
lib/
base.php
template.php
etc.
public/
index.php
以下命令足以启动您的服务器并将请求正确地分派(dispatch)给框架:
php -S 0.0.0.0:8090 -t public/
或者如果您直接从 public/文件夹运行命令:
cd public
php -S 0.0.0.0:8090
请注意,应用程序的工作目录取决于调用命令的文件夹。为了利用这个值,我强烈建议您在 public/index.php
文件的顶部添加 chdir(__DIR__);
。这样,所有后续的 require
调用都将与您的 public/
文件夹相关。例如:$f3 = require('../lib/base.php');
内置服务器默认不会将不存在的文件 URI 传递给您的index.php
,如下所述:
If a URI request does not specify a file, then either index.php or index.html in the given directory are returned
所以如果你打算用点来定义一些路由,比如:
$f3->route('GET /brew.json','Brew->json');
$f3->route('GET /brew.html','Brew->html');
那么它将无法工作,因为 PHP 不会将请求传递给 index.php
。
在这种情况下,您需要调用自定义路由器,例如您尝试使用的 .htrouter.php
。唯一的问题是你的 .htrouter.php
显然是为不同的框架设计的(F3 不关心 $_GET['url']
但关心 $_SERVER['SCRIPT_NAME']
。
这里有一个 .htrouter.php
的例子,它应该与 F3 一起工作:
// public directory definition
$public_dir=__DIR__.'/public';
// serve existing files as-is
if (file_exists($public_dir.$_SERVER['REQUEST_URI']))
return FALSE;
// patch SCRIPT_NAME and pass the request to index.php
$_SERVER['SCRIPT_NAME']='index.php';
require($public_dir.'/index.php');
注意:$public_dir
变量应根据 .htrouter.php
文件的位置进行设置。
例如,如果您调用:
php -S 0.0.0.0:8090 -t public/ .htrouter.php
它应该是 $public_dir=__DIR__.'/public'
。
但是如果你调用:
cd public
php -S 0.0.0.0:8090 .htrouter.php
它应该是 $public_dir=__DIR__
。
关于fat-free-framework - 使用 PHP 内置 Web 服务器的 Fatfree 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52102994/
这个问题在这里已经有了答案: How do free and malloc work in C? (8 个答案) 关闭 8 年前。 如果你使用malloc()为4个整数分配内存,它不应该返回第一个整
首先,介绍一下背景知识,这样您就不会认为我在尝试做一些疯狂的事情: 我正在尝试调试由其他人编写的 C 库中的崩溃。崩溃看起来像这样: TheProgram(44365,0x7fff75996310)
我正在 cstdlib malloc() 和 free() 函数之上创建自定义内存分配器(出于性能原因)。分配器位于一个简单的类中,该类存储一些内存缓冲区和其他参数。我想将释放内存的方法命名为 fre
我一直在解决这个练习,我不知道从哪里开始: 语言 B 是上下文无关的;语言 C 是 B 的子集:C 是否是上下文无关的?证明或反驳。 我试过使用闭包属性: C = B - ( (A* - C) ∩ B
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
如果我想获得在 C 中进行 malloc 的指针的所有权,则 docs for the Python cffi package和 this answer假设使用 ffi.gc 和 lib.free 作
#include #include struct node { int value; struct node* next; }; typedef struct node node_
众所周知,Oracle 在 Java 11 中更改了 Java 许可证,要求 JDK 的商业用途需要付费许可证。然而,使用 OpenJDK 仍然是免费的。 我的 PC 上有一个 JDK 11 文件夹,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
在多线程编程中,我们可以找到两个或多个线程/任务之间的数据传输同步的不同术语。 什么时候我们可以说某个算法是: 1)Lock-Free 2)Wait-Free 3)Wait-Freedom 我明白无锁
我正在尝试使用我通过 malloc() 手动分配的数组来运行程序。我在程序末尾释放()这个数组,但我不断收到错误消息 *** glibc detector *** ./test: double fre
我将 libxml2 与 libxslt 一起用于 C++ 程序的 XML 处理。为了使用 XSL 转换 XML 文档,我使用了以下函数(删除了错误处理): xmlDocPtr transformXm
new/delete 关键字使用免费商店 malloc/free 关键字是使用堆 我看到某处写着new 使用malloc。怎么会这样?它们不在内存段中使用? 其次,我看到某处写道我们不能在new 之后
我有这个简单的代码,我想在 tutorialspoint.com 上运行 #include using namespace std; class Vehicle { string vehic
我需要创建一个函数来删除 c 中链表的前 n 个节点,并返回删除的节点数。如果列表小于 n,它应该变为空。 另外,我不能使用递归。 使用现在的代码,它可以工作,但我没有释放“已删除”节点的内存。如果我
我需要调试这段代码的帮助。我知道问题出在 malloc 和 free 中,但找不到确切的位置、原因和解决方法。请不要回答:“使用 gdb”,仅此而已。我会使用 gdb 来调试它,但我仍然不太了解它并且
这个问题在这里已经有了答案: Unable to free const pointers in C (12 个答案) 关闭 8 年前。 将 C++11 代码连接到某些 C 回调,我必须传递 cons
这是出于好奇,我试图找到我对之前问题的疑问的答案,但他们似乎没有答案。所以在这里问,我只是写了一个代码,我试图将内存分配给一个 int 指针(以填充一个数组)并将 int 值扫描到它。完成数组后,我想
我有两个免费的单子(monad),用于不同上下文中的不同操作。但是,如果特定操作位于上下文中,则一个(主要)DSL 需要包含另一个(action)DSL: import Control.Monad.F
我是一名优秀的程序员,十分优秀!