Sharing Images/assets On Yii2's Advanced Template

Hi, guys,

I’ve just installed the advanced template for yii-2 and i’ve noticed that its structure resembles Yii Boiler plate. Now I remember last time working on site that was dealing with Images, Multimedia et al (Articles with images) and I had a nightmare trying to upload them in Backend and show them on Front End (Suppose you do magazine Site). I’m just starting out today with Yii2 and would like to know if that is the case for Advanced template and if not how do I upload article with image (using WYSIWYG Editor) on BE and let user enjoy it in FE?

Sorry if its direct, am just excite how Yii2 is great and can’t wait to start developing with it!

It all depends on how you’re accessing uploaded assets. If you’re doing it directly then symlink could be a good idea. If assets are published via asset manager then it can handle it w/o additional actions from your side (except defining bundles).

Many ways to do this. One direct way to do this is to have a common web accessible folder under app root. Something like <approot>/images, which you can access from both BE and FE. You would need to edit your .htaccess to allow this as well.

Hi samdark,

My case looks like this: I have single magazine article that editor is uploading. the Editor is using Backend to Upload that image. The Normal reader will access it via Frontend. So how does publishing using assets manager work in this way? Am normally using ReedactorJs Yii extension to do the file uploads.

Concerning symlinks, It does not work since many live servers disables it.

Could you give a dummy pseudo example on how to access it on URL (I have no problem uploading it to that directory, only forming URL was the issue last time I tried - but then it was Boiler plate not Yii2 :))

For a default yii advanced app… you can store the absolute application baseUrl in yii params in your config file (this will be constant for your environment). For example:




// for your production machine

'params' => [

   'baseUrl' => 'http://www.yourapp.com'

]

// for your development machine

'params' => [

   'baseUrl' => 'http://localhost/yourapp'

]



Then you can generate your images using:




use yii\helpers\Html;

function getImage($filename, $options) {

    $options['src'] = Yii::$app->params['baseUrl'] . '/images/' . $filename;

    return Html::tag('img', '', $options);

}



Thanks a lot Kartik

How do I publish Images? In Guide I can see "css" and "js".

Publish entire directory.

@samdark

How do you find the published image folder?

For example I use an AssetBundle (SampleAsset extends AssetBundle) with a $sourcePath.

It has some public $css and $js arrays.

In the layout I register the bundle:

SampleAsset::register($this);

All the files get published to a web accessible folder underneath /web/assets

The css and js files work fine, since they are added to the layout’s html with pointers to the new published folder.

But I have no idea how to display an image from that folder.

Ok - I just figured it out… duh…

$bundle = SampleAsset::register($this);

<?= Html::img($bundle->baseUrl.’/images/superduper.jpg’)?>

The images are showed when I’m using the localhost but on a live host it’s showing broke images




'params' => [

        'baseUrl' => 'http://www.domainname.com/'

     ]






<?php echo Html::img(Yii::$app->params['baseUrl'] .'/uploads/images/projects/image.jpg', ['width'=>'350','height'=>'200'],['alt' => 'alt image']);?>



Please help