Amélioration de la configuration de l'UrlManager. Depuis chaque application, nous pouvons désormais accéder à l'UrlManager des autres apps. Par exemple accéder à UrlManagerFrontend depuis l'app "producer" pour faire un lien vers l'app "frontend".dev
require(__DIR__ . '/params-local.php') | require(__DIR__ . '/params-local.php') | ||||
); | ); | ||||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||||
return [ | return [ | ||||
'id' => 'app-backend', | 'id' => 'app-backend', | ||||
'basePath' => dirname(__DIR__), | 'basePath' => dirname(__DIR__), | ||||
'errorHandler' => [ | 'errorHandler' => [ | ||||
'errorAction' => 'site/error', | 'errorAction' => 'site/error', | ||||
], | ], | ||||
'urlManager' => $common_config_main['components']['urlManagerBackend'], | |||||
], | ], | ||||
'params' => $params, | 'params' => $params, | ||||
]; | ]; |
<?php | |||||
namespace common\components; | |||||
use Yii; | |||||
use yii\helpers\Url; | |||||
use yii\base\InvalidConfigException; | |||||
use yii\web\UrlManager ; | |||||
class UrlManagerCommon extends UrlManager | |||||
{ | |||||
public $subDomain; | |||||
public $domainName; | |||||
protected $_hostInfo; | |||||
public function getProperDomain() | |||||
{ | |||||
if ( ! isset($this->domainName) || empty($this->domainName) ) { | |||||
throw new InvalidConfigException('Request requires a domain name to be configured!'); | |||||
} | |||||
$subDomain = (isset($this->subDomain) && !empty($this->subDomain)) ? $this->subDomain : ''; | |||||
$domain = empty($subDomain) ? '' : $subDomain . '.'; | |||||
$domain .= $this->domainName; | |||||
return $domain; | |||||
} | |||||
public function getHostInfo() | |||||
{ | |||||
if ($this->_hostInfo === null) | |||||
{ | |||||
$secure = Yii::$app->getRequest()->getIsSecureConnection(); | |||||
$http = $secure ? 'https' : 'http'; | |||||
if (isset($_SERVER['HTTP_HOST'])) { | |||||
$this->_hostInfo = $http . '://' . $this->getProperDomain(); | |||||
} elseif (isset($_SERVER['SERVER_NAME'])) { | |||||
$this->_hostInfo = $http . '://' . $this->getProperDomain(); | |||||
$port = $secure ? $this->getSecurePort() : $this->getPort(); | |||||
if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) { | |||||
$this->_hostInfo .= ':' . $port; | |||||
} | |||||
} | |||||
} | |||||
return $this->_hostInfo; | |||||
} | |||||
} |
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console'); | Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console'); | ||||
Yii::setAlias('producer', dirname(dirname(__DIR__)) . '/producer'); | Yii::setAlias('producer', dirname(dirname(__DIR__)) . '/producer'); | ||||
$server_name = $_SERVER['SERVER_NAME'] ; | |||||
Yii::setAlias('@domainName', (YII_ENV === 'dev') ? (($server_name == 'localhost') ? 'localhost' : 'laboiteapain-dev.net') : 'laboiteapain.net'); | |||||
Yii::setAlias('@baseUrl', ($server_name == 'localhost') ? '/reservation_pain/' : '/'); | |||||
Yii::setAlias('@baseUrlFrontend', (($server_name == 'localhost') ? '/frontend/web' : '')); | |||||
Yii::setAlias('@baseUrlBackend', (($server_name == 'localhost') ? '/backend/web' : '')); | |||||
Yii::setAlias('@baseUrlProducer', (($server_name == 'localhost') ? '/producer/web' : '')); | |||||
Yii::setAlias('@frontendSubdomain', (($server_name == 'localhost') ? '' : 'www')); | |||||
Yii::setAlias('@backendSubdomain', (($server_name == 'localhost') ? '' : 'admin')); | |||||
Yii::setAlias('@producerSubdomain', (($server_name == 'localhost') ? '' : 'producteurs')); | |||||
/* | /* | ||||
* Autoload | * Autoload | ||||
*/ | */ | ||||
Import::using('yii\helpers\*'); | Import::using('yii\helpers\*'); | ||||
Import::using('yii\web\NotFoundHttpException'); | Import::using('yii\web\NotFoundHttpException'); | ||||
// models | // models | ||||
Import::using('common\models\*'); | Import::using('common\models\*'); | ||||
Import::using('frontend\models\*'); | Import::using('frontend\models\*'); | ||||
// components | // components | ||||
Import::using('common\components\*'); | Import::using('common\components\*'); | ||||
Import::using('producer\components\*'); | Import::using('producer\components\*'); | ||||
Import::using('backend\components\*'); | |||||
Import::using('frontend\components\*'); | |||||
Import::using('kartik\mpdf\Pdf'); | |||||
Import::using('kartik\mpdf\Pdf'); |
'class' => 'yii\image\ImageDriver', | 'class' => 'yii\image\ImageDriver', | ||||
'driver' => 'GD', //GD or Imagick | 'driver' => 'GD', //GD or Imagick | ||||
], | ], | ||||
'urlManagerProducer' => [ | |||||
'class' => 'producer\components\UrlManagerProducer', | |||||
'subDomain' => Yii::getAlias('@producerSubdomain'), | |||||
'domainName' => Yii::getAlias('@domainName'), | |||||
'baseUrl' => Yii::getAlias('@baseUrl').Yii::getAlias('@baseUrlProducer'), | |||||
'enablePrettyUrl' => true, | |||||
'showScriptName' => false, | |||||
'enableStrictParsing' => false, | |||||
'rules' => [ | |||||
'<slug_producer:\w+>/<controller>/<action>' => '<controller>/<action>', | |||||
], | |||||
], | |||||
'urlManagerFrontend' => [ | |||||
'class' => 'common\components\UrlManagerCommon', | |||||
'subDomain' => Yii::getAlias('@frontendSubdomain'), | |||||
'domainName' => Yii::getAlias('@domainName'), | |||||
'baseUrl' => Yii::getAlias('@baseUrl').Yii::getAlias('@baseUrlFrontend'), | |||||
'enablePrettyUrl' => true, | |||||
'showScriptName' => false, | |||||
'enableStrictParsing' => false, | |||||
'rules' => [ | |||||
], | |||||
], | |||||
'urlManagerBackend' => [ | |||||
'class' => 'common\components\UrlManagerCommon', | |||||
'subDomain' => Yii::getAlias('@backendSubdomain'), | |||||
'domainName' => Yii::getAlias('@domainName'), | |||||
'baseUrl' => Yii::getAlias('@baseUrl').Yii::getAlias('@baseUrlBackend'), | |||||
'enablePrettyUrl' => true, | |||||
'showScriptName' => false, | |||||
'enableStrictParsing' => false, | |||||
'rules' => [ | |||||
], | |||||
], | |||||
], | ], | ||||
'language' => 'fr-FR', | 'language' => 'fr-FR', | ||||
]; | ]; |
require(__DIR__ . '/params-local.php') | require(__DIR__ . '/params-local.php') | ||||
); | ); | ||||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||||
return [ | return [ | ||||
'id' => 'app-frontend', | 'id' => 'app-frontend', | ||||
'basePath' => dirname(__DIR__), | 'basePath' => dirname(__DIR__), | ||||
], | ], | ||||
'view' => [ | 'view' => [ | ||||
'class' => 'common\components\MyView', | 'class' => 'common\components\MyView', | ||||
] | |||||
], | |||||
'urlManager' => $common_config_main['components']['urlManagerFrontend'], | |||||
], | ], | ||||
'params' => $params, | 'params' => $params, | ||||
]; | ]; |
namespace producer\components ; | namespace producer\components ; | ||||
class ProducerUrlManager extends \yii\web\UrlManager { | |||||
class UrlManagerProducer extends \common\components\UrlManagerCommon { | |||||
public function createUrl($params) { | public function createUrl($params) { | ||||
if(!is_array($params)) | if(!is_array($params)) |
require(__DIR__ . '/params-local.php') | require(__DIR__ . '/params-local.php') | ||||
); | ); | ||||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||||
return [ | return [ | ||||
'id' => 'app-producer', | 'id' => 'app-producer', | ||||
'basePath' => dirname(__DIR__), | 'basePath' => dirname(__DIR__), | ||||
'view' => [ | 'view' => [ | ||||
'class' => 'producer\components\ProducerView', | 'class' => 'producer\components\ProducerView', | ||||
], | ], | ||||
'urlManager' => [ | |||||
'class' => 'producer\components\ProducerUrlManager', | |||||
'enablePrettyUrl' => true, | |||||
'showScriptName' => false, | |||||
'enableStrictParsing' => false, | |||||
'rules' => [ | |||||
'<slug_producer:\w+>/<controller>/<action>' => '<controller>/<action>', | |||||
], | |||||
], | |||||
'urlManager' => $common_config_main['components']['urlManagerProducer'], | |||||
], | ], | ||||
'params' => $params, | 'params' => $params, | ||||
]; | ]; |
'items' => [ | 'items' => [ | ||||
[ | [ | ||||
'label' => '<span class="glyphicon glyphicon-user"></span> Inscription', | 'label' => '<span class="glyphicon glyphicon-user"></span> Inscription', | ||||
'url' => Url::frontend('site/signup'), | |||||
'url' => Yii::$app->urlManagerFrontend->createUrl(['site/signup']), | |||||
'visible' => Yii::$app->user->isGuest | 'visible' => Yii::$app->user->isGuest | ||||
], | ], | ||||
[ | [ | ||||
'label' => '<span class="glyphicon glyphicon-log-in"></span> Connexion', | 'label' => '<span class="glyphicon glyphicon-log-in"></span> Connexion', | ||||
'url' => Url::frontend('site/login'), | |||||
'url' => Yii::$app->urlManagerFrontend->createUrl(['site/login','return_url' => Yii::$app->urlManager->createUrl(['producer/index'])]), | |||||
'visible' => Yii::$app->user->isGuest | 'visible' => Yii::$app->user->isGuest | ||||
], | ], | ||||
[ | [ |