Answer the question
In order to leave comments, you need to log in
How to create link from backend to frontend in Yii2?
There is an advanced application, the backend is located on the admin.site.dev subdomain, the site is located on site.dev.
You need to redirect the user from the admin panel to the main site.
For this, an additional UrlManager has been introduced in this way:
// в backend components
'reverseUrlManager' => [
'class' => yii\web\UrlManager::className(),
'baseUrl' => 'http://site.dev',
'enablePrettyUrl' => false,
'showScriptName' => true,
],
// в frontend components
'reverseUrlManager' => [
'class' => yii\web\UrlManager::className(),
'baseUrl' => 'http://admin.site.dev',
'enablePrettyUrl' => false,
'showScriptName' => true,
],
// backend
/* @var $urlManager UrlManager */
$urlManager = \Yii::$app->get('reverseUrlManager');
var_dump($urlManager->createAbsoluteUrl(['/hello']));
/* @var $urlManager UrlManager */
$urlManager = \Yii::$app->get('reverseUrlManager');
$urlManager->showScriptName = false;
$urlManager->enablePrettyUrl = true;
var_dump($urlManager->createAbsoluteUrl(['/site']));
http://site.dev/hello
Works! showScriptName = false
and enablePrettyUrl = true
?
Answer the question
In order to leave comments, you need to log in
In general, who would be interested in how to solve this:
'reverseUrlManager' => [
'class' => yii\web\UrlManager::className(),
'baseUrl' => 'http://site.dev',
'scriptUrl' => 'http://site.dev', // нужно добавить это
'enablePrettyUrl' => false,
'showScriptName' => true,
],
and like this:
$link = Yii::$app->reverseUrlManager->createUrl(['site/index']);
var_dump($link);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question