简体   繁体   English

等效于 php 中的 asp.net 的 ResolveUrl

[英]Equivalent of asp.net's ResolveUrl in php

In my apache www dir, I have subdirectory for different personal projects I work on.在我的 apache www 目录中,我有我从事的不同个人项目的子目录。 Ex:前任:

www/webApp1 www/webApp2 www/webApp1 www/webApp2

I access to webApp2 by http://localhost:81/webApp2 (I currently run a portable wamp, that's why I'm on port 81. It does not matter right now...)我通过http://localhost:81/webApp2访问 webApp2 (我目前运行便携式 wamp,这就是我在端口 81 上的原因。现在没关系......)

I'm not using virtual host here, maybe I should, but I don't know much about it.我在这里没有使用虚拟主机,也许我应该使用,但我对此了解不多。

So for my webbApp2, I have the file util/files.php with the following function:所以对于我的 webbApp2,我有文件util/files.php和以下 function:

function BaseUrl ()
{
    $baseDir = dirname(dirname(__FILE__));
    $pos = strrpos($baseDir, '\\');
    if (!$pos)
    {
        $pos = strrpos($baseDir, '/');
    }
    $theDir = substr ($baseDir, $pos + 1);
    if ($theDir == 'public_html')
    {
        $theDir = '~johnny5'; //Hmmmmm...
    }
    return 'http://'.$_SERVER["HTTP_HOST"].'/'.$theDir;
}

I can call this method from any php file to "resolve" an url.我可以从任何 php 文件中调用此方法来“解决”url。

require_once("util/files.php");
$myUrl = BaseUrl ().'/someFolderAtTheRootOfWebApp2/myfile.css';
$css = $baseUrl.'/css/tab.css';

Then $css is "http://localhost:81/webApp2/someFolderAtTheRootOfWebApp2/myfile.css" .然后$css"http://localhost:81/webApp2/someFolderAtTheRootOfWebApp2/myfile.css" That way I can generate dynamically the links to my css or javascript files, for example.例如,这样我可以动态生成指向我的 css 或 javascript 文件的链接。

In asp.Net, I would write string url = Page.ResolveUrl ("~/folder/file.css");在 asp.Net 中,我会写string url = Page.ResolveUrl ("~/folder/file.css"); . .

It does works, but I wonder if there is a more elegant way to do this.它确实有效,但我想知道是否有更优雅的方式来做到这一点。 Maybe there's something built-in in php to handle that.也许 php 中内置了一些东西来处理这个问题。 And more important, you can see the patch with public_html to handle my userdir when I run the app under my Linux box.更重要的是,当我在 Linux 框下运行应用程序时,您可以看到带有public_html的补丁来处理我的用户目录。 That's not really portable.这不是真正的便携。

Any suggestions?有什么建议么?

What exactly do you want to do?你到底想做什么? Get an absolute url for a file?获取文件的绝对 url? If so you have to do the logic yourself - as php is not really integrated into apache is has no idea of your server structure.如果是这样,你必须自己做逻辑 - 因为 php 并没有真正集成到 apache 中,所以不知道你的服务器结构。

Is it a good idea to use ini_set and ini_get in this case?在这种情况下使用 ini_set 和 ini_get 是个好主意吗?

ini_set('include_class_path', 'http://'.$_SERVER["HTTP_HOST"]."/Project/lib/");
ini_set('include_controls_path', 'http://'.$_SERVER["HTTP_HOST"].'Project/controls/');
echo $get = ini_get('include_class_path');

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

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