Yii2 : Show image from Folder Common

前端 未结 3 1518
后悔当初
后悔当初 2020-12-21 16:57

I have a picture in folder common that i want to show in view, but it doesn\'t shown.

My code is :

request->bas         


        
相关标签:
3条回答
  • 2020-12-21 17:21

    Since no one actually posted an example of using assets to access files saved in a backend-folder or the other way around, it's quite simple:

    Let's say you are working with the yii advanced template and you have a backup/web/uploads/ folder in which you save images uploaded via your backend. Now you'd like to display those images on your frontend. Since the Frontend-App and the Backend-App are two seperate Applications, they don't know of each other.

    You create a new asset in your frontend/assets/, let's call it BackendAsset.php:

    <?php
    
       namespace frontend\assets;
       use yii\web\AssetBundle;
    
       class BackendAsset extends AssetBundle {
          public $sourcePath = '@backend/web/uploads';
       }
    

    where $sourcePath is the backend-folder (source) you'd like to access on the frontend. The @backend alias is predefined in the advanced template, so we'll use it.

    Now in our view we can simply use the BackendAsset:

    <?php
    
        use frontend\assets\BackendAsset;
        $backend = BackendAsset::register($this);
    
    ?>
    

    and now we can easily display a file, let's say backend/web/uploads/somefile.jpg:

    <img src="<?= $backend->baseUrl . '/somefile.jpg' ?>" >
    

    NOTE: Using the asset in this way copies all the files from the backend/web/uploads to an asset folder in the frontend. To prevent that, you can tell your application not to copy the files, but to link to them (creating SymLinks) instead, unsing linkAssets (yii2 docu):

    In your app configuration (in this case frontend/config/main.php), set the linkAssets parameter to TRUE:

    'components' => [
        'assetManager' => [
            'linkAssets' => true,
            ]
        ]
    
    0 讨论(0)
  • 2020-12-21 17:33

    We can do it easily by simple step of below In common/config/params-local.php

    Yii::setAlias('@imageurl', 'http://localhost/project_name/backend/web/');
    

    in above example i have my image under backend/web/images/product_images

    In frontend, i am getting like,

    <img src="<?php echo Yii ::getAlias('@imageurl'); ?>/images/product_images/<?= $product['image']?>" alt=" " class="img-responsive" />
    

    it's simply working for me...

    0 讨论(0)
  • 2020-12-21 17:34

    In case of using advanced application template, images should be placed in these web accessible folders - frontend/web or backend/web.

    Also usually I create alias from frontend/web/images to backend/web/images to display images in backend from frontend.

    What you mentioned is not web accessible directory.

    Alternative way to publish images from such directory will be creating asset bundle for that folder, that way images will be copied in frontend/web/assets for example. You can read more about asset bundles in official docs.

    0 讨论(0)
提交回复
热议问题