Php 在调用控制器文件夹中包含文件的更好方法(根控制器有文件夹)
在工作的PHP MVC上,在控制器文件夹上我有一个文件控制器,但里面有一些文件夹必须称为文件夹内的控制器 有没有更好的方法和代码实现下面的这些 我正在研究这种方式,但我不确定这是呼叫控制器的最佳方式吗Php 在调用控制器文件夹中包含文件的更好方法(根控制器有文件夹),php,model-view-controller,path,controller,include,Php,Model View Controller,Path,Controller,Include,在工作的PHP MVC上,在控制器文件夹上我有一个文件控制器,但里面有一些文件夹必须称为文件夹内的控制器 有没有更好的方法和代码实现下面的这些 我正在研究这种方式,但我不确定这是呼叫控制器的最佳方式吗 /* STRUCTURE OF DIR FILES: controllers: a/b/c/file.php a/b/c <-- IS DIRECTORY a/b/c <-- IS FILE */ $uri_result = false; $controller_called =
/*
STRUCTURE OF DIR FILES: controllers:
a/b/c/file.php
a/b/c <-- IS DIRECTORY
a/b/c <-- IS FILE
*/
$uri_result = false;
$controller_called = false;
/* called controllers */
$uri_segments[0] = 'a';
$uri_segments[1] = 'b';
$uri_segments[2] = 'c';
#$uri_segments[3] = 'file.php';
/* end called controllers */
$counted = count($uri_segments);
$filled = array();
$i = 0;
do {
if ($i < $counted)
{
$z[] = $uri_segments[$i];
$ez = implode('/', $z);
}
if (file_exists($ez))
{
$uri_result = $ez;
$controller_called = $z[$i];
}
++$i;
} while ($i < $counted);
var_dump($uri_result,$controller_called);
/* RESULTS:
If called $uri_segments[0] to uri_segments[3]
string(14) "a/b/c/file.php" string(8) "file.php"
If called $uri_segments[0] to uri_segments[2]
string(5) "a/b/c" string(1) "c"
*/
基本的方法似乎还可以,但我需要整理一些东西,包括更好的变量名和范围,以及使用更合适的循环 另外,从我所看到的,您希望找到URL路径的最长部分作为控制器,因此可能希望从长开始变短,而不是从短开始变长
/**
* Find the most specific controller file to serve a particular URL path
*
* @param string $url_path Relative path passed through from URL handler
* @return string $controller_file Relative path to controller file
*/
function find_controller_file($url_path)
{
$url_parts = explode('/', $url_path);
// Start with the full path, and remove sections until we find a controller file
while ( count($url_parts) > 0 )
{
// Concatenate remaining parts of path; this will get shorter on each loop
$test_path = implode('/', $url_parts) . '.php';
if ( file_exists($test_path) )
{
// We've found a controller! Look no further!
return $test_path;
}
else
{
// No controller there. Let's remove the last path part and try again
array_pop($url_parts);
}
}
// If we reach here, we never found a controller. This is probably an error.
throw new Controller_Not_Found_Exception();
}
根据hakra在下面的评论接受后进行编辑,要包含的文件可能始终具有扩展名.php,所以现在的示例反映了这一点。我不清楚您在这里想要实现什么。这段代码的实际输入是什么?您是否试图根据类名查找要加载的文件?还是基于URL?为什么需要测试多种可能性?我已经完成了包含控制器类、查找类名end等。。但是,在控制器文件夹中,我希望在控制器文件夹中创建包含控制器文件的文件。示例如果控制器文件夹中不存在文件,则应在URI调用的文件夹中搜索uri1不存在文件控制器,但在文件夹控制器中找到uri2,而不是在路由模式下找到uri2,是否应该在调用>之前包含并检查控制器文件,以便您的函数将URL作为输入,文件名作为输出?是。输入是由/分解的数组的$uri_段。若存在文件,则称为控制器,但若不存在,则应在文件夹内检查控制器文件。上面的例子是可行的,但我认为应该优化和更好的方法,不包括路由。而且,我对否决票感到消极惊讶。。。这是假的否决票!我无法解释这些否决票,但控制器所在的PHP文件应该具有.PHP文件扩展名或类似文件扩展名。在这种情况下,情况并非可疑。还要注意,有些恶意数据可以通过URI注入,但并非所有Web服务器都会对其进行规范化。所以这实际上可能是一件冒险的事情。这两个观点都很好。添加.php是一件小事,因此我已将其添加到示例代码中。转义/规范化更为复杂,但我同意,在这个函数中可能应该有一些不信任输入,以及所有这些。首先,我想您可以使用正则表达式来测试$url\u路径中的非字母数字。是的,但我需要一部分代码,而不是how include文件、how include扩展名等。。。我使用的是名为“.php”扩展名的EX常量,因为脚本可能与php5或其他东西一起使用。。。关于恶意数据,我还使用预加载类来防止xss和uri的清除,所以这不是问题。问题是优化代码约定。我需要一些代码,不是全部。并且在uri中使用不带.php扩展名,因为我在.htaccess策略上使用的是不带uri扩展名的。谢谢