I can upload and see images by admin panel in back-end application without any problems and see images in the "frontend" yii2 application also, but I have an additional separate front-end application and I can't see images there. It's returns 404 error.
Take one image, for example, that has one URL in all applications, but all applications have different domain names.
My common/config/main.php :
'modules' => [
'yii2images' => [
'class' => 'rico\yii2images\Module',
//be sure, that permissions ok
//if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
'imagesStorePath' => '@root/upload/store', //path to origin images
'imagesCachePath' => '@root/upload/cache', //path to resized copies
'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
'placeHolderPath' => '@root/upload/store/no-image.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
'imageCompressionQuality' => 85, // Optional. Default value is 85.
]
Same code of Costa-Rico/images in all 3 models:
public $gallery;
public $gallery_url;
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
In the separate frontend application I trying to show image by with code:
$general_logo = General::find()->where(['index' => 'logo_social'])->one();
if($general_logo) $image = $general_logo->getImage();
if($general_logo && $image) : ?>
<meta property="og:image" content="<?= $image->getUrl(); ?>"/>
<?php endif; ?>
I was trying to use frontend\models\General and creating of own this_separate_application\models\General (ActiveRecord, same table) for this application.
How I can to solve this problem? Thank you.
If I understand correctly, you want to access an image from the back-end in the front-end. And they both have different domains. If that is indeed the case, you can just do this in the front-end, while replacing BACKEND_URL
with the URL of the back-end. Let me know how it goes.
<meta property="og:image" content="<?= BACKEND_URL . $image->getUrl(); ?>"/>
I find the mistake. It was the stupped problem in the routing of my additional application. In the file separate_application/config/main.php:
'urlManager' => [
'rules' => [
[
'pattern' => '<url:.+>',
'route' => 'page/view',
],
],
]
This rule sent all URL patterns, that don't fit previous rules, to the controller PageController that is responsible for display of separated text pages. PageController also returned 404 page. In this way, it captured URL before it was processed by extension CostaRico/yii2-images and sent the script wrong way. This rule was deleted. Everithing is ok with images now!