-
Notifications
You must be signed in to change notification settings - Fork 3
/
UrlRule.php
52 lines (44 loc) · 1.24 KB
/
UrlRule.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
<?php
namespace junqi\urlalias;
use yii\web\UrlRule as BaseUrlRule;
use junqi\urlalias\models\UrlAlias;
/**
* custom url rule
*/
class UrlRule extends BaseUrlRule
{
/** @var string $pattern */
public $pattern = '';
/** @var string $route */
public $route = '';
/**
* @inheritdoc
* @param \yii\web\UrlManager $manager
* @param \yii\web\Request $request
* @return array|bool
*/
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
$urlAlias = UrlAlias::getRouteByAlias($pathInfo);
if (is_object($urlAlias)) {
return [$urlAlias->attributes['route'], unserialize($urlAlias->attributes['params'])];
}
return parent::parseRequest($manager, $request);
}
/**
* @inheritdoc
* @param \yii\web\UrlManager $manager
* @param string $route
* @param array $params
* @return string|bool
*/
public function createUrl($manager, $route, $params)
{
$urlAlias = UrlAlias::getAliasByRouteWithParams($route, $params);
if (is_object($urlAlias)) {
return $urlAlias->attributes['alias'];
}
return parent::createUrl($manager, $route, $params);
}
}