S
S
Sergey Beloventsev2016-07-17 16:51:34
Yii
Sergey Beloventsev, 2016-07-17 16:51:34

Is it possible to use multiple classes of UrlManager rules?

there are two UrlManager rule classes first

<?php
    namespace frontend\components;
    class FilmUrlRule extends Object implements UrlRuleInterface
    {
    
        public function createUrl($manager, $route, $params)
        {
            if ($route === 'film/category/onefilm') {
                if (isset($params['id'])) {
                    return 'film/'.$params['id'].'/';
                }
            }elseif($route === 'film/category/list'){
                if(isset($params['slug'])&&isset($params['page'])&&isset($params['per-page'])){
                    return 'film/'.$params['slug'].'?page='.$params['page'].'&per-page='.$params['per-page'].'/';
                }
                elseif ($route === 'film/category/list'&&isset($params['slug'])) {
                    return 'film/'.$params['slug'].'/';
            }
        }
            return false;  // данное правило не применимо
        }
    
        public function parseRequest($manager, $request)
        {
            $pathInfo = $request->getPathInfo();
            if (preg_match('%^(\w+)(/([\wʹ]*))?$%', $pathInfo, $matches)) {
                $exists = Category::find()->where(['slug_category' => $matches[3]])->exists();
                if ($exists) {
                    return ['film/category/list', ['slug' => $matches[3]]];
                }
            } elseif (preg_match('%^(\w+)(/(\w+)([+-/:/;]*([\wʹ]*)*)*)%', $pathInfo, $matches)) {
                $path = $matches[2];
                $pattern = '/film+[+-]/';
                if (preg_match($pattern, $path)) {
                    $exists = Film::find()->where(['slug_film' => str_replace('/','',$matches[2])])->exists();
                    if ($exists) {
                        return ['film/category/onefilm',['id'=>str_replace('/','',$matches[2])]];
                    }
                }else{
                    $exists = Category::find()->where(['slug_category' => str_replace('/','',$matches[2])])->exists();
                    if ($exists) {
                        return ['film/category/list', ['slug' => str_replace('/','',$matches[2])]];
                    }
    
                }
    
            }elseif (preg_match('/film/\/', $pathInfo)){
                return ['film/category/list'];
            }else{
                return false;  // данное правило не применимо
            }
            return false;
        }
    }

handles the following requests
Yii::$app->urlManager->createUrl(['/serial/category/oneserial','id'=>$model->slug_serial])
     Yii::$app->urlManager->createUrl(['/serial/category/list', 'slug' => $sct->slug_category])

and such a class
<?php
namespace frontend\components;
class SerialsUrlRule extends Object implements UrlRuleInterface
{

    public function createUrl($manager, $route, $params)
    {
        /*var_dump($route);*/
        if ($route === 'serial/category/oneserial') {
            if (isset($params['id'])) {
                return 'serials/'.$params['id'].'/';
            }
        }elseif($route === 'serial/category/list'){
            if(isset($params['slug'])&&isset($params['page'])&&isset($params['per-page'])){
                return 'serials/'.$params['slug'].'?page='.$params['page'].'&per-page='.$params['per-page'].'/';
            }
            elseif ($route === 'serial/category/list'&&isset($params['slug'])) {
                return 'serials/'.$params['slug'].'/';
        }
    }
        return false;  // данное правило не применимо
    }

    public function parseRequest($manager, $request)
    {
        $pathInfo = $request->getPathInfo();
        if (preg_match('%^(\w+)(/([\wʹ]*))?$%', $pathInfo, $matches)) {
            $exists = Category::find()->where(['slug_category' => $matches[3]])->exists();
            if ($exists) {
                return ['serial/category/list', ['slug' => $matches[3]]];
            }
        } elseif (preg_match('%^(\w+)(/(\w+)([+-/:/;]*([\wʹ]*)*)*)%', $pathInfo, $matches)) {
            $path = $matches[2];
            $pattern = '/serial+[+-]/';
            if (preg_match($pattern, $path)) {
                $exists = Serial::find()->where(['slug_serial' => str_replace('/','',$matches[2])])->exists();
                if ($exists) {
                    return ['serial/category/oneserial',['id'=>str_replace('/','',$matches[2])]];
                }
            }else{
                $exists = Category::find()->where(['slug_category' => str_replace('/','',$matches[2])])->exists();
                if ($exists) {
                    return ['serial/category/list', ['slug' => str_replace('/','',$matches[2])]];
                }

            }

        }elseif (preg_match('/serials/\/', $pathInfo)){
            return ['serial/category/list'];
        }else{
            return false;  // данное правило не применимо
        }
        return false;
    }
}

handles the following requests
Yii::$app->urlManager->createUrl(['/film/category/oneserial','id'=>$model->slug_serial])
     Yii::$app->urlManager->createUrl(['/film/category/list', 'slug' => $sct->slug_category])

connected like this
'rules' => [
                [
                        'class' => 'frontend\components\FilmUrlRule',
                    ],
                    [
                        'class' => 'frontend\components\SerialsUrlRule',
                    ],
    ]

but now if this rule works well
Yii::$app->urlManager->createUrl(['/film/category/oneserial','id'=>$model->slug_serial])
         Yii::$app->urlManager->createUrl(['/film/category/list', 'slug' => $sct->slug_category])

that rule
Yii::$app->urlManager->createUrl(['/serial/category/list', 'slug' => $sct->slug_category])

calls route `'/film/category/list'` why don't you tell me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-07-17
@Sergalas

Write 1 class, with different methods, or inherit one from the second, and that one is already from object
But in my opinion, this task is solved by the standard yii class and is configured in the rules

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                  '/'=>'site/index',
                  '/film/category/list/<slug>'=>'film/category/list',
                  ' /film/category/list'=>'film/category/all',

etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question