A
A
Absky2018-08-25 20:51:05
Yii
Absky, 2018-08-25 20:51:05

How to display an image using yii2 images in the main application loaded in a module?

Good afternoon,
I am loading and displaying images in posts using https://github.com/CostaRico/yii2-images.
Implemented loading of the image by the user from the module, and at the level of this module (personal account) the image is displayed correctly. when displayed in the main application, in the list of posts, a broken link icon is displayed.
I suspect that the absolute path from the module is not identical to the absolute path from the application.
How can the main application be forced to access the module path? Maybe someone came across.
How I output the image from the module to the main:
Write Controller:

class ProductController extends AppController{

    public function actionView($id){
        //$id = Yii::$app->request->get('id');
        $product = Product::findOne($id);

    if(empty($product)){
      throw new \yii\web\HttpException(404, 'Такой страницы не существует');
    }

        $hits = Product::find()->where(['hit' => '1'])->limit(3)->all();
        
        $this->setMeta('Maskota | '. $product->title, $product->keywords, $product->description);
        return $this->render('view', compact('product', 'hits'));
    }
}

Model:
namespace app\models;
use yii\db\ActiveRecord;


class Product extends ActiveRecord{
    public function behaviors()
    {
        return [
            'image' => [
                'class' => 'rico\yii2images\behaviors\ImageBehave',
            ]
        ];
    }

    public static function tableName(){
        return 'product';
    }

    public function getCategory(){
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }
}

View (here I perverted (see comments)):
use yii\helpers\Html;
use app\models\Category;
//подключаю модель продукта
use app\models\Product;
//подключаю модель продукта в модуле, где описывался личный кабинет, где все сделано в соответствии с документацией yii2-images и работает корректно
use app\modules\products\models\Products;
?>
<!-- cart_max -->
<div class="row">
  <div class="col-12 col-md-7">
    <ul class="cart_max">
  <?php 
//В Products ищу соответствующую $product->id запись($product - это $product = Product::findOne($id);, описан в actionView выше)

     $model = Products::findOne($product->id);
//к $model применяю функцию yii2-images getImage(); и получаю нужное изображение, вроде бы, норм
            $imageTitle = $model->getImage();
           ?>
//Вызов url и подстановка в img. Полученное url ведет к "/yii2images/images/image-by-item-and-alias?item=Products9&dirtyAlias=d1a282a7bb-1.png", к этому же пути ведет и изображение в модуле при вызове функции, но изображение отображается.
<li><?=Html::img("" . $imageTitle->getUrl(), ['alt' => $product->title]) ?></li>
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Absky, 2018-08-27
@Absky

The question is closed, I did everything right, just in the steam in the image storage folder I already cleaned up the images. Uploaded a new one - everything works. Making edits to the question, I noticed that the name of the image is strange.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question