在工作 PHP MVC 上,在 CONTROLLER 文件夹上我有一个文件控制器,但里面有必须在文件夹中调用控制器的文件夹。
下面有没有更好的方法和很好的代码实现?
我正在这样做,但我不确定这是调用控制器的最佳方式吗?
/*
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"
*/