Search code examples
phpimageyii2aliases

Yii2 show image in backend from frontend - settting alias


In my project I used an image cropper widget. I setup the widget, that it save in frontend/web/upload. But in backend I save the images to frontend too. This is working perfect. Then i want to show the image on the backend, if its exist. And i want to reach the frontend.

Thats why i want to set my own aliases in params-local.php file. But I using vhosts to my webpages and I want to set Aliases to them. In Yii2 documentation i found an article from aliases, but it wont help me. I mean i tried to use but it wont work.

I tried this:

return [
    'aliases' => [
        '@front' => 'http://front.mypage.dev',
        '@back' => 'http://back.mypage.dev',
    ],
];

And I also tried this aswell:

Yii::setAlias('@front', 'http://front.mypage.dev');
Yii::setAlias('@back', 'http://back.mypage.dev');

But when i try to echo Yii::getAlias('@front'); it sais

Invalid Parameter – yii\base\InvalidParamException

Invalid path alias: @front

Maybe someone has a solution for this?

Thanks a lot.


Solution

  • Add in backend/config/params.php like:

    return [
        'front' => 'http://front.mypage.dev',
        'back' => 'http://back.mypage.dev',
    ];
    

    and use it from:

    Yii::$app->params['front']
    Yii::$app->params['back']
    

    Let me know your thought.