|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\modules\seo; |
|
3
|
|
|
|
|
4
|
|
|
use app\components\BaseModule; |
|
5
|
|
|
use app\modules\seo\handlers\AnalyticsHandler; |
|
6
|
|
|
use app\modules\seo\models\Counter; |
|
7
|
|
|
use app\modules\seo\models\Meta; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\BootstrapInterface; |
|
10
|
|
|
use yii\web\Application; |
|
11
|
|
|
use yii\web\Controller; |
|
12
|
|
|
use yii\web\View; |
|
13
|
|
|
|
|
14
|
|
|
class SeoModule extends BaseModule implements BootstrapInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const NO_REDIRECT = 0; |
|
17
|
|
|
const FROM_WWW = 1; |
|
18
|
|
|
const FROM_WITHOUT_WWW = 2; |
|
19
|
|
|
public $cacheConfig = [ |
|
20
|
|
|
'metaCache' => [ |
|
21
|
|
|
'name' => 'metas', |
|
22
|
|
|
'expire' => 86400, |
|
23
|
|
|
], |
|
24
|
|
|
'counterCache' => [ |
|
25
|
|
|
'name' => 'counters', |
|
26
|
|
|
'expire' => 86400, |
|
27
|
|
|
], |
|
28
|
|
|
'robotsCache' => [ |
|
29
|
|
|
'name' => 'robots', |
|
30
|
|
|
'expire' => 86400, |
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
public $include = []; |
|
34
|
|
|
public $mainPage = ''; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var int type of redirect from WWW or without WWW |
|
37
|
|
|
*/ |
|
38
|
|
|
public $redirectWWW = self::NO_REDIRECT; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool if true redirect from url with trailing slash |
|
41
|
|
|
*/ |
|
42
|
|
|
public $redirectTrailingSlash = false; |
|
43
|
|
|
|
|
44
|
|
|
public $analytics = [ |
|
45
|
|
|
'ecGoogle' => [ |
|
46
|
|
|
'active' => 0, |
|
47
|
|
|
'currency' => AnalyticsHandler::CURRENCY_MAIN, |
|
48
|
|
|
], |
|
49
|
|
|
'ecYandex' => [ |
|
50
|
|
|
'active' => 0, |
|
51
|
|
|
'currency' => AnalyticsHandler::CURRENCY_MAIN, |
|
52
|
|
|
], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function bootstrap($app) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_string($this->include)) { |
|
61
|
|
|
$this->include = explode(',', $this->include); |
|
62
|
|
|
} |
|
63
|
|
|
$app->on( |
|
64
|
|
|
Application::EVENT_BEFORE_REQUEST, |
|
65
|
|
|
function () use ($app) { |
|
66
|
|
|
if ($app->getModule('seo')->redirectWWW != self::NO_REDIRECT) { |
|
67
|
|
|
self::redirectWWW(); |
|
68
|
|
|
} |
|
69
|
|
|
if ($app->getModule('seo')->redirectTrailingSlash == 1) { |
|
70
|
|
|
self::redirectSlash(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$app->getView()->on(View::EVENT_END_BODY, [Counter::className(), 'renderCounters'], $this->include); |
|
74
|
|
|
} |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
// Analytics |
|
78
|
|
|
$app->on(Application::EVENT_BEFORE_ACTION, [AnalyticsHandler::className(), 'handleBeforeAction']); |
|
79
|
|
|
$app->on(Application::EVENT_BEFORE_ACTION, [Meta::className(), 'registrationMeta']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function behaviors() |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
'configurableModule' => [ |
|
89
|
|
|
'class' => 'app\modules\config\behaviors\ConfigurableModuleBehavior', |
|
90
|
|
|
'configurationView' => '@app/modules/seo/views/configurable/_config', |
|
91
|
|
|
'configurableModel' => 'app\modules\seo\models\ConfigConfigurationModel', |
|
92
|
|
|
] |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return array prepared to dropdown list in configurable |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public static function getRedirectTypes() |
|
100
|
|
|
{ |
|
101
|
|
|
return [ |
|
102
|
|
|
self::NO_REDIRECT => Yii::t('app', 'No redirect'), |
|
103
|
|
|
self::FROM_WWW => Yii::t('app', 'Redirect from WWW to without WWW'), |
|
104
|
|
|
self::FROM_WITHOUT_WWW => Yii::t('app', 'Redirect from without WWW to WWW'), |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* If redirectWWW config make 301 redirect to www or not www domain |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function redirectWWW() |
|
112
|
|
|
{ |
|
113
|
|
|
$type = Yii::$app->getModule('seo')->redirectWWW; |
|
114
|
|
|
if ($type != self::NO_REDIRECT) { |
|
115
|
|
|
$readirArr = [ |
|
116
|
|
View Code Duplication |
self::FROM_WITHOUT_WWW => function () { |
|
117
|
|
|
if (preg_match('#^(http|https):\/\/www\.#i', Yii::$app->request->hostInfo) === 0) { |
|
118
|
|
|
Yii::$app->response->redirect( |
|
119
|
|
|
str_replace('://', '://www.', Yii::$app->request->absoluteUrl), |
|
120
|
|
|
301 |
|
121
|
|
|
); |
|
122
|
|
|
Yii::$app->end(); |
|
123
|
|
|
} |
|
124
|
|
|
}, |
|
125
|
|
View Code Duplication |
self::FROM_WWW => function () { |
|
126
|
|
|
if (preg_match('#^(http|https):\/\/www\.#i', Yii::$app->request->hostInfo) === 1) { |
|
127
|
|
|
Yii::$app->response->redirect( |
|
128
|
|
|
str_replace('://www.', '://', Yii::$app->request->absoluteUrl), |
|
129
|
|
|
301 |
|
130
|
|
|
); |
|
131
|
|
|
Yii::$app->end(); |
|
132
|
|
|
} |
|
133
|
|
|
}, |
|
134
|
|
|
]; |
|
135
|
|
|
$readirArr[$type](); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Make redirect from url with trailing slash |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function redirectSlash() |
|
143
|
|
|
{ |
|
144
|
|
|
$redirUrl = preg_replace('#^(.*)/$#', '$1', Yii::$app->request->url); |
|
145
|
|
|
if (!empty($redirUrl) && $redirUrl !== Yii::$app->request->url) { |
|
146
|
|
|
Yii::$app->response->redirect($redirUrl, 301); |
|
147
|
|
|
Yii::$app->end(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|