Yii2 : Show image from Folder Common

前端 未结 3 1534
后悔当初
后悔当初 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:

    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:

    
    

    and now we can easily display a file, let's say backend/web/uploads/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,
            ]
        ]
    

提交回复
热议问题