Answer the question
In order to leave comments, you need to log in
What's wrong with Yii2 routing?
made his own rules
<?php
namespace frontend\components\rules;
use Yii;
use yii\web\UrlRuleInterface;
use yii\base\BaseObject;
use common\models\InfoPage;
class InfoPageUrlRule extends BaseObject implements UrlRuleInterface
{
//public $route = 'infoPage/page';
public function createUrl($manager, $route, $params)
{
Yii::info($route);
if ($route == 'infoPage/page/' && !empty($params['id'])) {
//$url = false;//Yii::$app->cache->get('category-' . $params['id']);
//if (!$url) {
$infoPage = InfoPage::find()->where(['id' => $params['id']])->one();
if (!$infoPage) {
return false;
}
$url = '/' . $infoPage->slug.'/';
//Кэш на 3 часа
//Yii::$app->cache->set('category-' . $params['id'], $url, 10800);
//}
$param = '';
if (!empty($params['filter'])) {
foreach ($params['filter'] as $k => $v) {
$param .= ($param == '' ? '?' : '&') . $k . "=" . $v;
}
}
return $url . $param;
}
return false;
}
public function parseRequest($manager, $request)
{
if (empty($request->getPathInfo())) {
return false;
}
$infoPage = InfoPage::find()->where(['slug' => str_replace('/','',$request->getPathInfo()),])->one();
if ($infoPage) {
return ['infoPage/page', ['id' => $infoPage->id]];
}
Yii::info($infoPage);
return false;
}
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
'languages' => ['en', 'ru'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '/',
'rules' => [
['class' => 'frontend\components\rules\InfoPageUrlRule'],...
11:52:10.977 trace yii\web\Application::handleRequest Route requested: 'infoPage/page'
11:52:10.978 error yii\web\HttpException:404 yii\base\InvalidRouteException: Unable to resolve the request "infoPage/page". in /var/www/tiger.lc/vendor/yiisoft/yii2/base/Module.php:537
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question