Yii2 AssetManager Published Path Includes URL Schema

In my controller, I implemented a function to publish a single asset, such as an image, and returned 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 as follows:

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

Works fine, but the function returns the published URL without the "scheme" in front of it. For example, it returns /assets/7cf54cf2/img/logo.pnginsteadhttp://www.example.com/assets/7cf54cf2/img/logo.png

How can I use AssetManager or customize my code to get the full url? I cannot find the answer in the documentation, the only solution that I have come up with so far is to add it in front of me before returning.

I ran into this problem when trying to use this feature when creating HTML for email. Of course, the URLs in the email should contain the outline in front of it.

, ? !

+3
2

URL

use yii\helpers\Url;

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

, , css js?

:

\Yii::setAlias('@web', Url::base(true));
\Yii::$app->assetManager->baseUrl = Yii::getAlias('@web/assets');
0

Source: https://habr.com/ru/post/1570192/


All Articles