I
I
ImPuuLsE2015-01-20 08:49:44
Yii
ImPuuLsE, 2015-01-20 08:49:44

How to create UrlRule in Yii2?

Hello! I have the following rule ported from a yii 1 application... porting the module to yii2...

class PageUrlRule extends Object implements UrlRuleInterface
{
    public $connectionID = 'db';
    public $urlSuffix    = '.html';

    public function createUrl($manager, $route, $params)
    {
        if($route==='pages/pages/view')
        {
            $url=trim($params['url'],'/');
            unset($params['url']);

            $parts=array();
            if(!empty($params))
            {
                foreach ($params as $key=>$val)
                    $parts[]=$key.'/'.$val;

                $url .= '/'.implode('/', $parts);
            }

            return $url.$this->urlSuffix;
        }
        return false;
    }

    public function parseRequest($manager, $request)
    {
        $pathInfo = trim($request->getPathInfo(), '/');
        $pathInfo = str_replace($this->urlSuffix, '', $pathInfo);
        
        if(Pages::find()->where(['url' => $pathInfo])->one())

        {
            $_GET['url'] = $pathInfo;
            return 'pages/pages/view';
        }

        return false;
    }
}

gives an error:
d83d627e12694ec3ae442856e495c74a.PNG
How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Prosto, 2015-01-20
@ImPuuLsE

In public function parseRequest($manager, $request)
you return a string:
but there should be something like this
where in $params you put what you need

$params['id']=$page->id;
//или что вам там нужно
$params['url']=$page->url;

M
michaelromanov90, 2016-10-14
@michaelromanov90

I'm glad the toaster exists! This post was very helpful

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question