Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
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;
I'm glad the toaster exists! This post was very helpful
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question