SeoComponent.php 1.07 KB
<?php
namespace frontend\components;


use Yii;
use yii\base\BootstrapInterface;

class SeoComponent implements BootstrapInterface
{

    public function bootstrap($app)
    {
        \Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST, function($event) {
            $array = ['%21'=>'!','%22'=>'"','%23'=>'#','%24'=>'$','%25'=>'%','%26'=>'&','%27'=>'\'','%28'=>'(','%29'=>')','%2a'=>'*','%2b'=>'+','%2c'=>',','%2d'=>'-','%2e'=>'.','%2f'=>'/','%3a'=>':','%3b'=>';','%3c'=>'<','%3d'=>'=','%3e'=>'>','%3f'=>'?','%40'=>'@','%5b'=>'[','%5c'=>'\\','%5d'=>']','%5e'=>'^','%5f'=>'_','%60'=>'`','%7b'=>'{','%7c'=>'|','%7d'=>'}','%7e'=>'~'];
            $url = mb_strtolower (\Yii::$app->request->url);

            $continue = true;

            foreach($array as $sym=>$sym_row){
                if(strpos($url, $sym)){
                    $url = str_replace($sym, $sym_row, $url);
                    $continue = false;

                }
            }

            if(!$continue){
                \Yii::$app->getResponse()->redirect($url);
            }

        });
        return $app;
    }

}