简体   繁体   English

从URI和Web根提取公共路径

[英]Extract common path from URI and Web Root

I have a self-built MVC fw with a router routing URLs such that the common example.com/controller/action is used. 我有一个带有路由器路由URL的自建MVC fw,因此使用了常见的example.com/controller/action 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_app现在是控制器的名称,而controller是方法。

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 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? 从B删除/ my_app / pub并保留/ cntrl / actn最快的衬板是什么? Basically looking for a perl-esque way of getting the common denominator like string. 基本上是寻找一种Perl式的方式来获得像字符串这样的公分母。

Thanks for any input 感谢您的输入

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;
    }
}

The way I would solve this is adding this logic to the front controller (the file to which your server sends all nonexistant file requests, usually index.php). 我要解决的方法是将此逻辑添加到前端控制器 (服务器将所有不存在的文件请求发送到的文件,通常是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);

Here's a codepad demo . 这是一个键盘演示

What does this do? 这是做什么的? If the front controller is located (relative to the www root) in: /subdir/myapp/ , then it's $_SERVER['SCRIPT_NAME'] would be /subdir/myapp/index.php . 如果前端控制器位于/subdir/myapp/ (相对于www根目录),则$_SERVER['SCRIPT_NAME']/subdir/myapp/index.php The actual request URI is contained in $_SERVER['REQUEST_URI'] . 实际的请求URI包含在$_SERVER['REQUEST_URI'] Let's say, for example, that it is /subdir/myapp/controller/action?extras=stuff . 例如,假设它是/subdir/myapp/controller/action?extras=stuff To remove the subdirectory prefix we need to find the length of it. 要删除子目录前缀,我们需要找到它的长度。 That is found by subtracting the length of the script name (retrieved from basename() ) from the length of the script's name relative to the www root. 通过从相对于www根的脚本名称的长度中减去脚本名称的长度(从basename()检索basename()可以找到该值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM