Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yii2 (advanced app) - Creating frontend URLs in backend and vice versa
<?php
$config = [
'components' => [
// [...]
'request' => [
'baseUrl' => $params['backend.baseUrl'],
],
'urlManagerFrontend' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => $params['frontend.baseUrl'],
'hostInfo' => $params['frontend.protocol'] . '://' . $params['frontend.host'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => $params['frontend.urlrules'],
],
'urlManager' => [
'baseUrl' => $params['backend.baseUrl'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => $params['backend.urlrules'],
],
// [../]
],
);
<?php
namespace backend\controllers;
class SiteController extends Controller
{
public function actionIndex() {
/**
* Create absolute backend URL from backend app
*
* @var \yii\web\UrlManager $urlManager
*/
$urlManager = \Yii::$app->urlManager;
echo \yii\helpers\VarDumper::dumpAsString(
$urlManager->createAbsoluteUrl(['controller/action'])
);
/**
* Create absolute frontend URL from backend app
*
* @var \yii\web\UrlManager $urlManagerFrontend
*/
$urlManagerFrontend = \Yii::$app->urlManagerFrontend;
echo \yii\helpers\VarDumper::dumpAsString(
$urlManagerFrontend->createAbsoluteUrl(['controller/action'])
);
// Results in 'http://example.com/admin/controller/action''http://example.com/controller/action'
}
}
<?php
return [
// [...]
'frontend.baseUrl' => '',
'frontend.urlrules' => [
'/' => 'site/index',
],
'frontend.protocol' => 'http',
'frontend.host' => 'dev.example.com',
'backend.baseUrl' => '/admin',
'backend.urlrules' => [
'' => 'site/index',
'/' => 'site/index',
'/login' => 'site/login',
'/logout' => 'site/logout',
'/users' => 'users/index',
'/users/create' => 'users/create',
'/users/<id:\d+>' => 'users/view',
'/users/<id:\d+>/update' => 'users/update',
'/users/<id:\d+>/delete' => 'users/delete',
],
'backend.protocol' => 'http',
'frontend.host' => 'dev.example.com',
// [...]
];
<?php
$config = [
'components' => [
// [...]
'request' => [
'baseUrl' => $params['frontend.baseUrl'],
],
'urlManagerBackend' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => $params['backend.baseUrl'],
'hostInfo' => $params['backend.protocol'] . '://' . $params['backend.host'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => $params['backend.urlrules'],
],
'urlManager' => [
'baseUrl' => $params['frontend.baseUrl'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => $params['frontend.urlrules'],
],
// [../]
],
);
<?php
namespace frontend\controllers;
class SiteController extends Controller
{
public function actionIndex() {
/**
* Create absolute frontend URL from frontend app
*
* @var \yii\web\UrlManager $urlManager
*/
$urlManager = \Yii::$app->urlManager;
echo \yii\helpers\VarDumper::dumpAsString(
$urlManager->createAbsoluteUrl(['controller/action'])
);
/**
* Create absolute backend URL from frontend app
*
* @var \yii\web\UrlManager $urlManagerBackend
*/
$urlManagerBackend = \Yii::$app->urlManagerBackend;
echo \yii\helpers\VarDumper::dumpAsString(
$urlManagerBackend->createAbsoluteUrl(['controller/action'])
);
// Results in 'http://example.com/controller/action''http://example.com/admin/controller/action'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.