есть два класса правила UrlManager первый
<?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;
}
}
отрабатывает следующийе запросы
Yii::$app->urlManager->createUrl(['/serial/category/oneserial','id'=>$model->slug_serial])
Yii::$app->urlManager->createUrl(['/serial/category/list', 'slug' => $sct->slug_category])
и такой класс
<?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;
}
}
отрабатывает следующие запросы
Yii::$app->urlManager->createUrl(['/film/category/oneserial','id'=>$model->slug_serial])
Yii::$app->urlManager->createUrl(['/film/category/list', 'slug' => $sct->slug_category])
подключил так
'rules' => [
[
'class' => 'frontend\components\FilmUrlRule',
],
[
'class' => 'frontend\components\SerialsUrlRule',
],
]
но теперь если это правило отрабатывает хорошо
Yii::$app->urlManager->createUrl(['/film/category/oneserial','id'=>$model->slug_serial])
Yii::$app->urlManager->createUrl(['/film/category/list', 'slug' => $sct->slug_category])
то правило
Yii::$app->urlManager->createUrl(['/serial/category/list', 'slug' => $sct->slug_category])
вызывает route `'/film/category/list'` почему так не подскажите