Yii2 AssetManager发布的路径包含URL方案 [英] Yii2 AssetManager published path include URL scheme

查看:74
本文介绍了Yii2 AssetManager发布的路径包含URL方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我实现了一个用于发布单个资产(例如图像)并返回URL的功能:

In my controller I have implemented a function for publishing a single asset, such as an image, and return the URL:

/**
 * Publish an asset and return url
 *
 * @param $src
 *
 * @return mixed
 */
public function publishAsset( $src ) {
    $path = Yii::getAlias( $src );
    if ( ! $this->assetManager ) {
        $this->assetManager = new AssetManager();
    }
    $return = $this->assetManager->publish( $path );
    return $return[1];
}

然后在我的视图中这样称呼它:

And then call it in my view like this:

<?= $this->context->publishAsset('@app/assets/img/logo.png') ?>

工作正常,但是该函数返回已发布的U​​RL,但前面没有'scheme'. 例如,它返回/assets/7cf54cf2/img/logo.png而不是http://www.example.com/assets/7cf54cf2/img/logo.png

Working fine, but the function returns the published URL without the 'scheme' in front of it. For example it returns /assets/7cf54cf2/img/logo.png instead of http://www.example.com/assets/7cf54cf2/img/logo.png

如何使用AssetManager或调整代码,以获得完整的URL? 我在文档中找不到答案,到目前为止,我想出的唯一解决方案是在返回之前手动将其手动添加.

How can I use the AssetManager or adjust my code so I can get the full URL? I can't find an answer in the documentation, the only solution I have come up with so far is adding it in front manually before returning.

在尝试使用此功能为电子邮件生成HTML时遇到了这个问题.当然,电子邮件中的URL必须在其前面包含该方案.

I came onto this problem when trying to use this function in generating HTML for e-mails. Of course URLs in e-mails must contain the scheme in front of it.

除了我的建议以外,还有其他建议吗?谢谢!

Any suggestions other than mine? Thanks!

推荐答案

尝试使用url帮助程序

Try using url helper

use yii\helpers\Url;

$publishedPath = $this->assetManager->publish( $path );
   return (Url::to($publishedPath[1], true));

这篇关于Yii2 AssetManager发布的路径包含URL方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆