1 | <?php |
||
21 | class UrlNormalizer extends Object |
||
22 | { |
||
23 | /** |
||
24 | * Represents permament redirection during route normalization. |
||
25 | * @see https://en.wikipedia.org/wiki/HTTP_301 |
||
26 | */ |
||
27 | const ACTION_REDIRECT_PERMANENT = 301; |
||
28 | /** |
||
29 | * Represents temporary redirection during route normalization. |
||
30 | * @see https://en.wikipedia.org/wiki/HTTP_302 |
||
31 | */ |
||
32 | const ACTION_REDIRECT_TEMPORARY = 302; |
||
33 | /** |
||
34 | * Represents showing 404 error page during route normalization. |
||
35 | * @see https://en.wikipedia.org/wiki/HTTP_404 |
||
36 | */ |
||
37 | const ACTION_NOT_FOUND = 404; |
||
38 | |||
39 | /** |
||
40 | * @var boolean whether slashes should be collapsed, for example `site///index` will be |
||
41 | * converted into `site/index` |
||
42 | */ |
||
43 | public $collapseSlashes = true; |
||
44 | /** |
||
45 | * @var boolean whether trailing slash should be normalized according to the suffix settings |
||
46 | * of the rule |
||
47 | */ |
||
48 | public $normalizeTrailingSlash = true; |
||
49 | /** |
||
50 | * @var integer|callable|null action to perform during route normalization. |
||
51 | * Available options are: |
||
52 | * - `null` - no special action will be performed |
||
53 | * - `301` - the request should be redirected to the normalized URL using |
||
54 | * permanent redirection |
||
55 | * - `302` - the request should be redirected to the normalized URL using |
||
56 | * temporary redirection |
||
57 | * - `404` - [[NotFoundHttpException]] will be thrown |
||
58 | * - `callable` - custom user callback, for example: |
||
59 | * |
||
60 | * ```php |
||
61 | * function ($route, $normalizer) { |
||
62 | * // use custom action for redirections |
||
63 | * $route[1]['oldRoute'] = $route[0]; |
||
64 | * $route[0] = 'site/redirect'; |
||
65 | * return $route; |
||
66 | * } |
||
67 | * ``` |
||
68 | */ |
||
69 | public $action = self::ACTION_REDIRECT_PERMANENT; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Performs normalization action for the specified $route. |
||
74 | * @param array $route route for normalization |
||
75 | * @return array normalized route |
||
76 | * @throws InvalidConfigException if invalid normalization action is used. |
||
77 | * @throws UrlNormalizerRedirectException if normalization requires redirection. |
||
78 | * @throws NotFoundHttpException if normalization suggests action matching route does not exist. |
||
79 | */ |
||
80 | 5 | public function normalizeRoute($route) |
|
94 | |||
95 | /** |
||
96 | * Normalizes specified pathInfo. |
||
97 | * @param string $pathInfo pathInfo for normalization |
||
98 | * @param string $suffix current rule suffix |
||
99 | * @param boolean $normalized if specified, this variable will be set to `true` if $pathInfo |
||
100 | * was changed during normalization |
||
101 | * @return string normalized pathInfo |
||
102 | */ |
||
103 | 5 | public function normalizePathInfo($pathInfo, $suffix, &$normalized = false) |
|
122 | |||
123 | /** |
||
124 | * Collapse consecutive slashes in $pathInfo, for example converts `site///index` into `site/index`. |
||
125 | * @param string $pathInfo raw path info. |
||
126 | * @return string normalized path info. |
||
127 | */ |
||
128 | 5 | protected function collapseSlashes($pathInfo) |
|
132 | |||
133 | /** |
||
134 | * Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a |
||
135 | * trailing slash or not. |
||
136 | * @param string $pathInfo raw path info. |
||
137 | * @param string $suffix |
||
138 | * @return string normalized path info. |
||
139 | */ |
||
140 | 5 | protected function normalizeTrailingSlash($pathInfo, $suffix) |
|
150 | } |