2

I have a self-built MVC fw with a router routing URLs such that the common example.com/controller/action is used. I'm running into issues when my application is deployed within a sub-directory such as

example.com/my_app/controller/action/?var=value

My router thinks my_app is the name of the controller now and controller is the method.

My current solution is to manually ask for any sub directory name in a config at install. I'd like to do this manually. See my question below and let me know if I'm going about solving this the wrong way and asking the wrong question.

My question: if I have two paths, how do I truncate the common pieces from the end of one and remove it from the end of the other.

A = /var/www/my_app/pub B = /my_app/pub/cntrl/actn

What's your quickest one liner to remove /my_app/pub from B and remain with /cntrl/actn? Basically looking for a perl-esque way of getting the common denominator like string.

Thanks for any input

4

2 回答 2

3
my @physical_parts = split qr{/}, $physical_path;
my @logical_parts  = split qr{/}, $logical_path;

my @physical_suffix;
my @logical_prefix;
my $found = 0;
while (@physical_parts && @logical_parts) {
    unshift @physical_suffix, pop(@physical_parts);
    push @logical_prefix, shift(@logical_parts);

    if (@physical_suffix ~~ @logical_prefix) {
       $found = 1;
       last;
    }
}
于 2012-07-23T22:20:58.817 回答
1

我解决这个问题的方法是将此逻辑添加到前端控制器(您的服务器将所有不存在的文件请求发送到的文件,通常是 index.php)。

$fontControllerPath = $_SERVER['SCRIPT_NAME'];
$frontControllerPathLength = strlen($fontControllerPath);

$frontControllerFileName = basename($fontControllerPath);
$frontControllerFileNameLength = strlen($frontControllerFileName);

$subdirectoryLength = $frontControllerPathLength - $frontControllerFileNameLength;

$url = substr($_SERVER['REQUEST_URI'], $subdirectoryLength - 1);

这是一个键盘演示

这是做什么的?如果前端控制器位于(相对于 www 根目录):/subdir/myapp/,那么它将$_SERVER['SCRIPT_NAME']/subdir/myapp/index.php. 实际的请求 URI 包含在$_SERVER['REQUEST_URI']. 例如,假设它是/subdir/myapp/controller/action?extras=stuff. 要删除子目录前缀,我们需要找到它的长度。basename()这是通过从脚本名称相对于 www 根的长度中减去脚本名称的长度(从 检索)来找到的。

File that receives request: /subdir/myapp/index.php (length = 23)
Filename: index.php                                 (length = 9)
                                                                 -
-------------------------------------------------------------------
                                               14 chars to remove

/subdir/mpapp/controller/action?extras=stuff
              ^
              Cut off everything before here
于 2012-07-23T22:21:28.473 回答