瀏覽代碼

Gestion avancée des urls inter applications (frontend, backend, producer)

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
keun 6 年之前
父節點
當前提交
594c5a52c1
共有 9 個檔案被更改,包括 4372 行新增14 行删除
  1. +3
    -0
      backend/config/main.php
  2. +50
    -0
      common/components/UrlManagerCommon.php
  3. +14
    -1
      common/config/bootstrap.php
  4. +34
    -0
      common/config/main.php
  5. +4
    -1
      frontend/config/main.php
  6. +1
    -1
      producer/components/UrlManagerProducer.php
  7. +3
    -9
      producer/config/main.php
  8. +4261
    -0
      producer/runtime/logs/app.log
  9. +2
    -2
      producer/views/layouts/main.php

+ 3
- 0
backend/config/main.php 查看文件

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

+ 50
- 0
common/components/UrlManagerCommon.php 查看文件

@@ -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;
}

}

+ 14
- 1
common/config/bootstrap.php 查看文件

@@ -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\*');

// pdf
Import::using('kartik\mpdf\Pdf');
Import::using('kartik\mpdf\Pdf');

+ 34
- 0
common/config/main.php 查看文件

@@ -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',
];

+ 4
- 1
frontend/config/main.php 查看文件

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

producer/components/ProducerUrlManager.php → producer/components/UrlManagerProducer.php 查看文件

@@ -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))

+ 3
- 9
producer/config/main.php 查看文件

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

+ 4261
- 0
producer/runtime/logs/app.log
文件差異過大導致無法顯示
查看文件


+ 2
- 2
producer/views/layouts/main.php 查看文件

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

Loading…
取消
儲存