Php 自定义路由不';t工作不正常[F3]
自定义路由无法正常工作,始终路由到user.htm index.phpPhp 自定义路由不';t工作不正常[F3],php,fat-free-framework,Php,Fat Free Framework,自定义路由无法正常工作,始终路由到user.htm index.php $routes = [ "/" => "index.htm", "/user/id=@id" => "user.htm","/user/@id" => "user.htm", ]; foreach ($routes as $path => $file) { $f3->route("GET ".$path, function($f3){ global $file
$routes = [
"/" => "index.htm",
"/user/id=@id" => "user.htm","/user/@id" => "user.htm",
];
foreach ($routes as $path => $file) {
$f3->route("GET ".$path,
function($f3){
global $file,$path;
echo View::instance()->render($file);
}
);
}
试试这个:
$routes = [
"/" => "index.htm",
"/user/id=@id" => "user.htm",
"/user/@id" => "user.htm",
];
foreach ($routes as $path => $file)
{
$f3->route("GET " . $path,
function ($f3) use ($file)
{
echo View::instance()->render($file);
}
);
}
Bryan Velastegui的答案是正确的。但以下是代码不起作用的原因:
$f3->route()
将每个路由URI映射到一个函数(称为“路由处理程序”),而不执行它foreach
循环将以下值依次存储到$file
变量中:index.html
、user.htm
和user.htm
(再次)。因此,在循环结束时,$file
保存user.htm
$f3->run()
后,框架将执行与当前路由匹配的路由处理程序,该路由处理程序本身引用全局$file
变量,并保持user.htm
我建议您阅读有关的文档,以了解Bryan Velastegui的代码是如何工作的。工作正常,谢谢。但是你能详细解释一下为什么这个方法有效而我用的方法无效吗?再次感谢:谢谢你的朋友,我是新来的,没有得到很好的解释。