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".refactoring
@@ -6,6 +6,8 @@ $params = array_merge( | |||
require(__DIR__ . '/params-local.php') | |||
); | |||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||
return [ | |||
'id' => 'app-backend', | |||
'basePath' => dirname(__DIR__), | |||
@@ -30,6 +32,7 @@ return [ | |||
'errorHandler' => [ | |||
'errorAction' => 'site/error', | |||
], | |||
'urlManager' => $common_config_main['components']['urlManagerBackend'], | |||
], | |||
'params' => $params, | |||
]; |
@@ -0,0 +1,50 @@ | |||
<?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; | |||
} | |||
} |
@@ -5,6 +5,16 @@ Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend'); | |||
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console'); | |||
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 | |||
*/ | |||
@@ -20,6 +30,7 @@ Import::using('yii\behaviors\*'); | |||
Import::using('yii\helpers\*'); | |||
Import::using('yii\web\NotFoundHttpException'); | |||
// models | |||
Import::using('common\models\*'); | |||
Import::using('frontend\models\*'); | |||
@@ -37,6 +48,8 @@ Import::using('common\helpers\*'); | |||
// components | |||
Import::using('common\components\*'); | |||
Import::using('producer\components\*'); | |||
Import::using('backend\components\*'); | |||
Import::using('frontend\components\*'); | |||
Import::using('kartik\mpdf\Pdf'); | |||
Import::using('kartik\mpdf\Pdf'); |
@@ -9,6 +9,40 @@ return [ | |||
'class' => 'yii\image\ImageDriver', | |||
'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', | |||
]; |
@@ -6,6 +6,8 @@ $params = array_merge( | |||
require(__DIR__ . '/params-local.php') | |||
); | |||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||
return [ | |||
'id' => 'app-frontend', | |||
'basePath' => dirname(__DIR__), | |||
@@ -43,7 +45,8 @@ return [ | |||
], | |||
'view' => [ | |||
'class' => 'common\components\MyView', | |||
] | |||
], | |||
'urlManager' => $common_config_main['components']['urlManagerFrontend'], | |||
], | |||
'params' => $params, | |||
]; |
@@ -2,7 +2,7 @@ | |||
namespace producer\components ; | |||
class ProducerUrlManager extends \yii\web\UrlManager { | |||
class UrlManagerProducer extends \common\components\UrlManagerCommon { | |||
public function createUrl($params) { | |||
if(!is_array($params)) |
@@ -6,6 +6,8 @@ $params = array_merge( | |||
require(__DIR__ . '/params-local.php') | |||
); | |||
$common_config_main = array_merge(require(__DIR__ . '/../../common/config/main.php')); | |||
return [ | |||
'id' => 'app-producer', | |||
'basePath' => dirname(__DIR__), | |||
@@ -41,15 +43,7 @@ return [ | |||
'view' => [ | |||
'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, | |||
]; |
@@ -37,12 +37,12 @@ $producer = $this->getProducer() ; | |||
'items' => [ | |||
[ | |||
'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 | |||
], | |||
[ | |||
'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 | |||
], | |||
[ |