Last active
July 29, 2021 19:28
-
-
Save Deele/b0f85a3cfabbba15be97f71ff3a09513 to your computer and use it in GitHub Desktop.
Yii2 (advanced app) - Creating frontend URLs in backend and vice versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'], | |
], | |
// [../] | |
], | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
// [...] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'], | |
], | |
// [../] | |
], | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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