S
S
Sergey Beloventsev2018-07-04 14:59:25
This
Sergey Beloventsev, 2018-07-04 14:59:25

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;
    }

this is how I connected it in config/main.php
'urlManager' => [
            'class' => 'codemix\localeurls\UrlManager',
            'languages' => ['en', 'ru'],
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '/',
            'rules' => [
                ['class' => 'frontend\components\rules\InfoPageUrlRule'],...

but when I go to a well-formed link, I get the following error

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

Why don't you tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zxscv, 2018-07-04
@Sergalas

Из https://www.yiiframework.com/doc/api/2.0/yii-web-u...
$route string
The route. It should not have slashes at the beginning or the end.
$route == 'infoPage/page/' => $route == 'infoPage/page'

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question