D
D
Dmitry Kim2016-11-15 12:56:29
Yii
Dmitry Kim, 2016-11-15 12:56:29

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,
],

To get a link to the frontend from the backend, I do this:
// backend
/* @var $urlManager UrlManager */
$urlManager = \Yii::$app->get('reverseUrlManager');
var_dump($urlManager->createAbsoluteUrl(['/hello']));

At the same time, I expect to receive site.dev/hello, but I receive admin.site.dev/hello.
It sucks! And if so:
/* @var $urlManager UrlManager */
$urlManager = \Yii::$app->get('reverseUrlManager');
$urlManager->showScriptName = false;
$urlManager->enablePrettyUrl = true;
var_dump($urlManager->createAbsoluteUrl(['/site']));

http://site.dev/helloWorks!
What's a goat? Why is the domain correct only for showScriptName = falseand enablePrettyUrl = true?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kim, 2016-11-17
@kimono

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,
],

M
Maxim Timofeev, 2016-11-16
@webinar

and like this:

$link = Yii::$app->reverseUrlManager->createUrl(['site/index']);
var_dump($link);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question