You should configure a url manager like you do for the frontend app. The same set of URL rules should be used.
Hello
I don't see what you mean @qiangxue.
I don't see how to (in my backend) create an url to a frontend controller action, could you please provide an example ?
1) If Yii::$app->urlManager->createUrl('');
returns url as http://localhost/yii2/frontend/web/index.php
. This code was a part of file in frontend folder.
2) If Yii::$app->urlManager->createUrl('');
returns url as http://localhost/yii2/backend/web/index.php
. This code was a part of file in backend folder.
3) But if code is part of backend which should point to frontend, then we cannot use this
Yii::$app->urlManager->createUrl('./../frontend/web/');
instead use simple html 'a' tag as
<a href="./../../frontend/web/">Go To Frontend</a>
In your backend application config you should add additional 'UrlManager' component with different name and configuration equals to the one used at front end application:
return [
'components' => [
'urlManager' => [
// here is your backend URL rules
],
'urlManagerFrontEnd' => [
// here is your Front-end URL rules
],
],
];
Then you should invoke following to compose front-end URL:
Yii::$app->urlManagerFrontEnd->createUrl();
@dilip-vishwa : thanks, but I knew this.
@klimov-paul : thanks, I was searching for this kind of solution.
I'll reopen it for documentation purpose.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'urlManagerFrontend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/a/frontend/web',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
Would be nice to have a few example to this, I use subdomain for backend and I can't figure out how to configure urlManagerFrontend to make this work. @djfly example didn't work for me, it just append /a/frontend/web to the backend URL.
paths problem, maybe your is /advanced/frontend/web
how to visit frontend/web? You paths different
To avoid duplicating rules, perhaps after $params definition in frontend/config/main.php (vice versa in backend)
$urlManagerBackEnd = require(__DIR__ . '/../../backend/config/urlManager.php');
$urlManager = require(__DIR__ . '/urlManager.php');
and then
'components' => [
...
'urlManager' => $urlManager,
'urlManagerBackEnd' => $urlManagerBackEnd,
],
where urlManager.php is something like this in the frontend
<?php
return [
'class' => 'yii\web\urlManager',
'baseUrl' => '//mydomain.com',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
...
],
];
and this in the backend (using admin subdomain)
<?php
return [
'class' => 'yii\web\urlManager',
'baseUrl' => '//admin.mydomain.com',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
...
],
];
I have created this as solution for me.. It is working for me in LOCALHOST... will it work on all scenarioes ???
//This is in backend/config/main.php
'urlManagerFrontend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '@web/../',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
Is it possible without adding in urlManagerFrontend
its possible but you have to create your own UrlManager in which you integrate tiers configurations & add a method in which you explictly point to the right tier.
Is there any chance we can do this without hardcoding the the baseUrl ?
'baseUrl' => '//admin.mydomain.com',
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question
._
Most helpful comment
In your backend application config you should add additional 'UrlManager' component with different name and configuration equals to the one used at front end application:
Then you should invoke following to compose front-end URL: