slick1537
(Kevin M Mckinney)
June 1, 2014, 11:12pm
1
I have a line of code like this
<h1> <?= Html::img('/yii2/basic/uploads/images/'.$model->id.'.'.$model->file_ext) ?></h1>
and it works. However if I upload it to a new server where it is no longer in the ‘yii2’ directory this code is going to break. Is there a better way of doing this? I’ve been reading about path aliases but I don’t really get how to set them up or use them.
Luciano
(Luciano Baraglia)
June 2, 2014, 4:05am
2
slick1537:
I have a line of code like this
<h1> <?= Html::img('/yii2/basic/uploads/images/'.$model->id.'.'.$model->file_ext) ?></h1>
and it works. However if I upload it to a new server where it is no longer in the ‘yii2’ directory this code is going to break. Is there a better way of doing this? I’ve been reading about path aliases but I don’t really get how to set them up or use them.
Hi, using/defining aliases is pretty simple, but for most thing you have predefined ones: https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-aliases.md#predefined-aliases-
One of them is @web alias, so you could do something like (in your view):
<h1><img src="<?= Yii::getAlias('@web') . '/images/' . $model->id . '.' . $model->file_ext ?>" /> </h1>
And you have to put your images under web accessible directory.
Your’s directory - /yii2/basic/uploads/images/ - does not look like the one.