-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aef3b8
commit 0dbea1a
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
|
||
namespace common\components\staticpages; | ||
|
||
use yii\web\CompositeUrlRule; | ||
|
||
/** | ||
* Description of StrictParseRequest | ||
* | ||
* @author VGRovnov | ||
*/ | ||
class StrictParseRequest extends CompositeUrlRule | ||
{ | ||
public $ruleConfig = ['class' => 'yii\web\UrlRule']; | ||
public $onlyGET = true; | ||
|
||
protected function createRules() | ||
{ | ||
$verb = NULL; | ||
if($this->onlyGET) | ||
{ | ||
$verb = 'GET'; | ||
} | ||
|
||
return [ | ||
\Yii::createObject(array_merge($this->ruleConfig, [ | ||
'pattern' => '<m>/<c>/<a>', | ||
'route' => '<m>/<c>/<a>', | ||
'verb' => $verb, | ||
])), | ||
|
||
\Yii::createObject(array_merge($this->ruleConfig, [ | ||
'pattern' => '<c>/<a>', | ||
'route' => '<c>/<a>', | ||
'verb' => $verb, | ||
])) | ||
]; | ||
} | ||
|
||
|
||
public function __wakeup() | ||
{ | ||
$this->init(); | ||
} | ||
|
||
public function parseRequest($manager, $request) | ||
{ | ||
$result = parent::parseRequest($manager, $request); | ||
|
||
if (empty($result)) | ||
{ | ||
return $result; | ||
} | ||
|
||
$url = array_merge(["/".$result[0]], $result[1], $request->getQueryParams()); | ||
|
||
$canonical = $manager->createUrl($url); | ||
|
||
if($request->url != $canonical) { | ||
\Yii::$app->response->redirect($canonical, 301); | ||
} | ||
|
||
return $result; | ||
} | ||
} |