/
WebsiteBehaviorUrlRule.php
53 lines (45 loc) · 1.19 KB
/
WebsiteBehaviorUrlRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace luya\cms\frontend\components;
use luya\helpers\Url;
use luya\web\UrlRule;
use Yii;
use yii\web\UrlNormalizerRedirectException;
/**
* Host routing rule for websites.
*
* UrlRule to enable routing behavior for website hosts.
*
* @author Bennet Klarhoelter <boehsermoe@me.com>
* @since 4.0.0
*/
class WebsiteBehaviorUrlRule extends UrlRule
{
/**
* @inheritdoc
*/
public $pattern = '<module>/<controller>/<action>';
/**
* @inheritdoc
*/
public $route = '<module>/<controller>/<action>';
/**
* @inheritdoc
*/
public $defaults = ['controller' => 'default', 'action' => 'index'];
/**
* @inheritdoc
*/
public $mode = UrlRule::PARSING_ONLY;
/**
* @inheritdoc
*/
public function parseRequest($manager, $request)
{
$matchedWebsite = Yii::$app->website->findOneByHostName($request->hostName);
if (boolval($matchedWebsite['redirect_to_host']) && $matchedWebsite['host'] !== $request->hostName) {
$request->setHostInfo($matchedWebsite['host']);
throw new UrlNormalizerRedirectException(Url::to($request->getAbsoluteUrl()), 301);
}
return false;
}
}