Reputation: 335
I have an application developped using Yii Framework v2.
In one of the pages, images are included, but don't display properly as their url contains the local server IP address, and not the public server name. For example, I have img
tag with src
like https://10.0.0.20/path/to/image.jpg.
As I understand it (I'm not the one who developped this), the URL is set using the following function, from backend\modules\news\models\News.php:
public function getImageUrl()
{
$path = \Yii::getAlias('@uploads/news/') . $this->image;
if (!is_file($path)) {
return '';
} else {
return Url::to(\Yii::getAlias('@uploads/news-rel/' . $this->image), true);
}
}
It is called in a file frontend/widgets/news/views/_item.php with:
<?php
use kartik\helpers\Html;
use common\helpers\DateHelper;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $model backend\modules\news\models\News */
setlocale(LC_TIME, 'fr_FR.UTF-8');
?>
<div class="news-item-image col-md-3">
<?php if ($model->image): ?>
<?= Html::img($model->getImageUrl()) ?>
<?php endif; ?>
</div>
<div class="news-item-content col-md-9">
<div class="news-item-content-title"><?= $model->title ?></div>
<div class="news-item-content-date">
<?= strftime('%e %B %G', strtotime($model->publishDate)) ?>
</div>
<div class="news-item-content-body"><?= StringHelper::truncateWords(strip_tags($model->body), 20) ?></div>
<?= Html::a(Yii::t('app', 'Read more'), ['/news/view', 'id' => $model->id], ['class' => 'news-item-content-read-more']) ?>
</div>
In other places, same kind of code is used, and return a proper url (https://www.example.com/path/to/resource). What should I look after to debug this ?
EDIT1: @uploads alias is defined in backend/config/bootstrap.php with this lines:
/*Yii::setAlias('@uploads', realpath(dirname(__FILE__).'/../web') . '/uploads');
Yii::setAlias('uploads/images-rel', '/uploads/images');
Yii::setAlias('uploads/editor-rel', '/uploads/editor');*/
It is the same alias that is used in many other places, and returns urls with server name. Thanks Franck
Upvotes: 0
Views: 834
Reputation: 82
check your config/web.php
file.
Look for the hostInfo
Or set it up yourself. It's going to alter your Urls.
Also:
When $scheme is specified (either a string or true), an absolute URL with host info (obtained from yii\web\UrlManager::$hostInfo) will be returned. If $url is already an absolute URL, its scheme will be replaced with the specified one.
Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-url
Upvotes: 1