【问题标题】:Better way for including file in calling controller folder (root controller has folders)在调用控制器文件夹中包含文件的更好方法(根控制器有文件夹)
【发布时间】:2012-08-09 00:14:29
【问题描述】:

在工作 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" 

*/

【问题讨论】:

  • 我不清楚你想在这里实现什么。这部分代码的实际输入是什么?您是否试图根据类名查找要加载的文件?还是基于 URL?为什么需要测试多种可能性?
  • 我已经完成了包含控制器类,找到类名结束等。但是,在控制器文件夹中,我想在控制器文件夹中创建包含控制器文件的文件。例如,如果控制器文件夹中不存在文件,则应在 URI uri1/uri2/uri3 uri1 调用的文件夹内搜索没有文件控制器,但在文件夹控制器中找到 uri2 而不是 uri2 是控制器文件。
  • 此示例,但不在路由模式下,应在调用之前包含并检查控制器文件 > stackoverflow.com/questions/6529026/…
  • 所以你的函数会把 URL 作为输入和文件名作为输出?
  • 是的。输入是 $uri_segments 作为由“/”分解的数组。如果存在文件而不是称为控制器,但如果不存在则应检查文件夹内的控制器文件。上面的例子有效,但我认为应该优化和更好的方法,不包括路由。

标签: php model-view-controller path controller include


【解决方案1】:

基本方法似乎还可以,但我需要整理一些东西,包括更好的变量名和范围,以及使用更合适的循环。

此外,据我所见,您希望找到 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”,因此示例现在反映了这一点。

【讨论】:

  • 另外,我对downvote 感到失望......这是假的downvote!
  • 我无法解释反对意见,但控制器所在的 PHP 文件应该具有 .php 文件扩展名或类似名称。在这种情况下,这不仅仅是可疑的情况。另请注意,一些恶意数据可以通过 URI 注入,并非所有网络服务器都会对其进行规范化。所以这实际上可能是一件有风险的事情。
  • 这两点都很好。添加“.php”是一件小事,所以我将它添加到我的示例代码中。转义/规范化涉及更多,但我同意这个函数中可能应该有一些(永远不要相信输入,等等)。我想你可以使用正则表达式来测试$url_path 的非字母数字,作为开始。
  • 是的,但我需要一部分代码,而不是如何包含文件、如何包含扩展名等...我正在使用名为“.php”扩展名的 EX 常量,因为脚本可能与 php5 或其他东西一起使用...关于恶意数据,我也在使用预加载类来预防 xss 和 uri 消毒,所以这不是问题。问题是优化代码约定。我需要部分代码而不是全部。并且在 uri 中使用没有 .php 扩展名,因为我正在使用 .htaccess 策略而不在 URI 上显示扩展名。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多