Link To Backend Application

I want to put a link on my frontend app called ‘Admin’, which links to the \backend\web\index.php file … how should I change the NavBar url?




            $menuItems = [

                ['label' => 'Home', 'url' => ['/site/index']],

                ['label' => 'About', 'url' => ['@frontend/site/about']],

                ['label' => 'Admin', 'url' => ['/site/admin']],

            ];



The only way I have found thus far is to create an alias in the frontend app that is set to the actual URL of the backend app:




Yii::setAlias('backend', 'http://backend.dev');



I then set the label as follows in the frontend:




            $menuItems = [

                ['label' => 'Home', 'url' => ['/site/index']],

                ['label' => 'About', 'url' => [yii\helpers\Url::to('@backend')]],

                ['label' => 'Admin', 'url' => ['/site/admin']],

            ];



Is there a better way to do this?

Since they are different apps (backend and frontend) - it is difficult to use Yii app methods between 2 apps. The easier approach for menu items is to use absolute url links like you have in your scenario.

You can however design a component at the common level to return such items for both apps.

I have the same problem, so I am reviving this topic.

So far, I only managed to figure out something like below:

$backendUrl = str_replace('frontend', 'backend', Url::to(['site/index'], true));
echo Html::a('Control Panel', $backendUrl, ['class' => ['btn btn-danger']]);

Is there a better or more professional way of achieving above?

I have already tried the approach with aliases (actually, it was the first thing that I thought about).

Either I am doing something wrong, or your solution isn’t going to work (isn’t working on my side). Aliases are path aliases, not route aliases. I need those from the second group.

This is going to produce a link like (my Windows dev env):

c:\XAMPP\htdocs\company-name\project-name\backend/site/index

While I need:

localhost/company-name/project-name/backend/site/index

So full route. Where (of course) the localhost part is correctly replaced by current server’s URL on each of the environments (but that is actually handled gracefully by Yii 2).

So, either I am missing your point or false path.

Yep, my fault . Written in a hurry without focus on topic…

Config alias in your configuration:

'aliases' => [
  '@backend' => 'backend',
]

Use it:

Html::a('link', Url::to(['@backend/site/index', 'param' => 'value'])); 

(answer totally rewritten)

OK, first of all, I had to change your idea into using @back and @front aliases, because @backend and @frontend aliases are already defined in common/config/bootstrap.php file. And since they sit in bootstrap, they get higher precedence over things defined in main.php.

So, without this modification I was getting (out of your Url::to(['@backend/site/index', 'param' => 'value']) a link like:

http://localhost/company/project/backend/web/C:/XAMPP/htdocs/company/project/backend/site/index?param=value

So, I made this change and replaced first sub-array of common/config/main.php from:

'aliases' => [
    '@bower' => '@vendor/bower-asset',
    '@npm'   => '@vendor/npm-asset',
],

Into:

'aliases' => [
    '@npm'   => '@vendor/npm-asset',
    '@bower' => '@vendor/bower-asset',
    '@back' => 'backend',
    '@front' => 'frontend',
],

Then calling your code produced a link like:

http://localhost/company/project/backend/web/backend/site/index?param=value

I think that the biggest challenge here is to get full URL to the other application, while sitting in the first one. As Kartik has written, this may be impossible due to complete separation of these two applications in the ecosystem of Yii 2 App Advanced Theme-based apps.

My suggestion works, because this is a dirty string replacement. I am starting to think that a cleaner or more professional way may not exist.

You are right. I did some simple test with currently opened project based on basic template so I overlooked it.

Try to change defined route to absolute one:

'@back' => '/backend',
'@front' => '/frontend',

In my little test it works, omitting the module’s route part. This should work as expected.
See https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#toRoute()-detail

[…] A route may be either absolute or relative. An absolute route has a leading slash (e.g. /site/index), while a relative route has none (e.g. site/index or index).

Nope, still doesn’t work. Still produces:

http://localhost/company/project/backend/web/frontend/site/index?param=value

It works in your case and omits module, because module is inside current app. Here, I am trying to link cross-application, so outside current app.

As per my knowledge, both effects (that it works for you and that it doesn’t work for me) are matching expectations (i.e. how Url:to() works and how it is described).

I don’t think that we can figure much more here. I need to link cross-application (from backend to frontend and back) for just a single button, so my code (which works; with nasty str_replace) shall be enough.

I just wanted to make myself sure, if I am not trying to open a door with a knife instead of a key.

Last resort for aliases :slight_smile:

In this case, when you have two distinct app (thus the domains e.g.) you still can define them as aliases (absolute). This way you don’t have to remember where in code you did your dirty job :slight_smile: with string replace in case your urls change completely in the future.

With your last answer you have finally opened my eyes! Domains… ;]

I have realized that I have skipped domain configuration in Yii 2 Advanced Project Template’s Installation chapter. If this is completed and both backend and frontend application are accessible through different domains (or subdomains) then creating links from backend to frontend is a piece of cake.

In my configuration a single domain is used and my frontend and backed applications are accessible through different sub-folders of the same domain. Therefore linking between requires some “weird” code, like we tried to achieve here.

1 Like