0

我正在尝试从 URL 获取控件名称并将其与 index.php 中的正确控制器动态匹配。

我的应用程序根目录:/localhost/app/index.php

所以基本上当我输入 /localhost/app/index.php/home 时,试图包含 home_controller.php。这是代码;

 $parts = array_slice(explode('/',$_SERVER["REQUEST_URI"]),3);
 if(file_exists(dirname(__FILE__)."/controllers/".$parts[0].'_controller.php'))
 {
    include dirname(__FILE__)."/controllers/".$parts[0].'_controller.php';
 }

如果应用程序根深度为 3,它可以正常工作。当我更改应用程序目录(如 localhost/first/second/app/index.php/home)时它会损坏

因为它爆炸了 3,$parts[0] 不再是“家”问题是我怎样才能以更有效的方式检测控制器部件?

4

2 回答 2

1

我使用 $_SERVER paht_info 变量来计算,控制器部分..

  $segments = array_slice(explode("/", $_SERVER["PATH_INFO"] ),1);

$segment 数组的零索引是控制器部分,其他部分是函数。顺便说一句,您需要检查数组边界以及是否设置了 path_info。

于 2011-10-08T12:37:20.347 回答
0

/localhost/app/index.php/home localhost/first/second/app/index.php/home sounds like its making your string the $parts[0] piece look like app/index.php/home which would be an invalid path. The only way to handle this is either explode that string with / as your delimiter or using the last one in the array as the location to compare or run through each. The concept of MVC though is the URL is always structured similar part one, home in this case required to determin controller, 2+ are usually used in refrence to parameters to a function from the controller

于 2011-10-07T22:15:36.177 回答