There are two classes of rules UrlManager
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; } }
fulfills the following queries:
Yii::$app->urlManager->createUrl(['/serial/category/oneserial','id'=>$model->slug_serial]) Yii::$app->urlManager->createUrl(['/serial/category/list', 'slug' => $sct->slug_category])
and this class:
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; }
}
fulfills the following queries:
Yii::$app->urlManager->createUrl(['/film/category/oneserial','id'=>$model->slug_serial]) Yii::$app->urlManager->createUrl(['/film/category/list', 'slug' => $sct->slug_category])
hooked up so
'rules' => [ [ 'class' => 'frontend\components\FilmUrlRule', ], [ 'class' => 'frontend\components\SerialsUrlRule', ], ]
but now if this rule works out 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])
causes route '/film/category/list'
why not tell me that
urlManager
will perfectly cope with your rules. Why do you two managers do not understand, too, why not write one of your own with private methodsgetSerialCategoryUrl
andgetFilmCategoryUrl
andgetFilmCategoryUrl
them in the defaultcreateUrl
. - Makarenko_I_V