Answer the question
In order to leave comments, you need to log in
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;
}
}
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])
Answer the question
In order to leave comments, you need to log in
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',
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question