adnen manssouri
adnen manssouri

Reputation: 51

How to check is it a backend URL

'urlManagerBackend' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' =>  'http://backend.test',
        'enablePrettyUrl' => true,
        'showScriptName' => true,
    ],

then I want to display the image saved under uploads directory

<img src="<?= Yii::$app->urlManagerBackend->baseUrl; ?>/uploads/logo.jpg>

the problem is this url must not be hardcode like this:

'baseUrl' =>  'http://backend.test',

Upvotes: -1

Views: 8491

Answers (6)

Peter Aichinger
Peter Aichinger

Reputation: 1

What about putting a reverseproxy (e.g. nginx) in front of the frontend-server?
Could be configured like:

http://frontend/backend/* -> forwards everyhing to the backend service, the rest will still go to the frontend server.

The configuration (in this case the location of the backend server) of this reverseproxy can be changed any time (also after deployment).

Could that be a viable scenario?

Upvotes: -1

adnen manssouri
adnen manssouri

Reputation: 51

I solve this problem by saving the full url in the database.

Upvotes: 0

Michal Hynčica
Michal Hynčica

Reputation: 6144

The only way how to dynamically determine the domain of the other application (for example the backend from your frontend) would be by parsing the web server's configuration files.

The domain for current application (the one you can get with Url::base(true)) is determined from the request headers or variables set by web server. But those are available only for current application, not for any other application even if they are part of same project.

If you want to parse web server's configuration files than you will have to face three major challenges:

  1. Different web servers have different syntax for configuration files.
  2. Configuration files might be located anywhere.
  3. You might not have access rights to read the configuration files.

So it might be better to try to think about some workaround instead of insisting on determining the domain dynamically.

  1. Make a deploy script that would ask for the backend domain. The one who will be deploying your application on production servers will know the domain for the backend application and can enter it during deployment process. The deploy script will then set the entered backend domain in your configuration files.
  2. Make a page in backend that must be visited before accessing the frontend application. You can determine the domain for backend when the page in backend is visited then set that domain in frontend configuration files. If the frontend is accessed before the domain for backend is set you will only display the notice that the backend page must be accessed first.

Upvotes: 1

David Lang
David Lang

Reputation: 103

You could simply use Assets and Aliases for this:

If you have a backup/web/uploads/ folder in which you save images uploaded via your backend and you'd like to display those images on your frontend.

Create a new asset file 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,
        ]
    ]

Upvotes: 0

borisaeric
borisaeric

Reputation: 583

Too long comment so I need to put it here.

But I'm just wondering in which case that makes sense, except if you are creating web applications, sites, ..., through your application, which I doubt you do. You know your local domain (use local environment and put urls). You will know your dev domain (use dev environment and put urls). You will know your production domain (use prod environment and put urls).

You can also have multiple applications inside yii2 project, so for example, 10 applications across 3 envs, that is 30 urls which you will enter in you configs.

Can you please tell me, how you will access your app if url is dynamically determined -> without using anything else except Yii?

What is your step? You are typing in your browser what? Then we can proceed. Maybe we misunderstand each other.

urlManagerBackend' => [
    'class' => 'yii\web\urlManager',
    'baseUrl' =>  'http://backend.test',
    'enablePrettyUrl' => true,
    'showScriptName' => true,
]

If you are wondering you can also have multiple urlManagerBackend components across Yii2 environments. Just like with params. Add it on multiple corresponding places at config. So in specific environment you place at same file only key => values which you need to override.

Upvotes: 0

Mike
Mike

Reputation: 182

In the config folder there should be a file called params.php. If you have something like this

<?php

return [
    'adminEmail' => '[email protected]',
    'baseUrl' =>  'http://backend.test',
];

You can use it in your code like this

<img src="<?= Yii::$app->params['baseUrl']; ?>/uploads/logo.jpg>

Then when you move to live, you just need to edit the params.php file.

Upvotes: 0

Related Questions