php 使用 Zend 框架创建绝对 URL 的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1881254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:09:12  来源:igfitidea点击:

Best practice to create absolute URLs with Zend framework?

phpzend-frameworkhyperlink

提问by ck.

Is there a best practice for creating absolute URLs using the Zend framework? I wonder if there is some helper or if this would just be concatenating the scheme, host, etc. from the $_SERVER variable and then add the relative path generated by Zend.

是否有使用 Zend 框架创建绝对 URL 的最佳实践?我想知道是否有一些帮助程序,或者这是否只是将 $_SERVER 变量中的方案、主机等连接起来,然后添加 Zend 生成的相对路径。

回答by Tomá? Fejfar

phpfour's way is OK, but you have to check for https://, ftp:// and mailto: too... :)

phpfour 的方式是可以的,但是你也必须检查 https://、ftp:// 和 mailto: ...... :)

I prefefer having all urls root-absolute (/files/js/jquery.js). The "hardcore zend way" is

我更喜欢将所有 url 设为 root-absolute ( /files/js/jquery.js)。“铁杆zend方式”是

<?php 
// document root for example.com is in /htdocs 
// but application's index.php resides in /htdocs/myapp/public
echo $this->baseUrl('css/base.css'); 
//will return /myapp/public/css/base.css

echo $this->serverUrl() . $this->baseUrl('css/base.css');
//will return http://www.example.com/myapp/public/css/base.css

echo '//' . $this->getHelper('ServerUrl')->getHost() . $this->baseUrl('css/base.css');
//will return protocol relative URL //www.example.com/myapp/public/css/base.css

回答by Mr Coder

Without mvc

没有 mvc

echo $this->serverUrl() . $this->baseUrl('cass/base.css');

or with mvc

或使用 mvc

echo  $this->serverUrl() . $this->url(array('controller'=>'index','action'=>'index'),null,true);

回答by Mohammad Emran Hasan

In my applications, I keep a "baseUrl" in my application config and I assign that to registry on bootstrapping. Later I use the following View Helper to generate the URL:

在我的应用程序中,我在应用程序配置中保留了一个“baseUrl”,并在引导时将其分配给注册表。后来我使用下面的 View Helper 来生成 URL:

<?php

class Zend_View_Helper_UrlMap
{
    public function UrlMap($original)
    {
        $newUrl  = $original;
        $baseUrl = Zend_Registry::get('baseUrl');

        if (strpos($newUrl, "http://") === false) {
            $newUrl = $baseUrl . $newUrl;
        }

        return $newUrl;
    }
}

Benefit: I can make any change on all the URLs in the view from one place.

好处:我可以从一个地方对视图中的所有 URL 进行任何更改。

Hope this helps.

希望这可以帮助。

回答by Dmitry

Use the below method.

使用下面的方法。

<a href="<?php echo $this->serverUrl() . $this->url('application', ['action' => 'detail']) ?>">

<a href="<?php echo $this->serverUrl() . $this->url('application', ['action' => 'detail']) ?>">

回答by prodigitalson

I wouldnt use $_SERVER, I would use the values from Zend_Controller_Request_Http::getServer($keyName);(or the direct getters on the request object when that applies - i forget which ones are direct members of the object and which ones need to be accessed with getServer). Technically these are the exact same values, but IMO its better access it the Zend way than to use the raw PHP access. But yes catting those together should get you what you need. This is actually the way i did it for an SSL controller plugin/url helper.

我不会使用 $_SERVER,我会使用来自Zend_Controller_Request_Http::getServer($keyName);(或请求对象上的直接 getter的值,当适用时 - 我忘记了哪些是对象的直接成员,哪些需要使用 getServer 访问)。从技术上讲,这些是完全相同的值,但 IMO 以 Zend 方式访问它比使用原始 PHP 访问更好。但是,是的,将它们放在一起应该可以满足您的需求。这实际上是我为 SSL 控制器插件/url 助手所做的方式。

There could be a better way though...

虽然可能有更好的方法......