Francis
Francis

Reputation: 1895

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.

Upvotes: 2

Views: 2861

Answers (2)

Insane Skull
Insane Skull

Reputation: 9368

Try this:

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

echo  Yii::getAlias('@front');
echo  Yii::getAlias('@back');
echo  Yii::getAlias('@frontend/path/to/file');
echo  Yii::getAlias('@backend/path/to/file');

Yii2 Playground
setting-aliases-in-yii2-within-the-app-config-file

Upvotes: 1

Ishan Shah
Ishan Shah

Reputation: 1676

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.

Upvotes: 3

Related Questions