A
A
abrakadana2016-05-31 12:40:06
Yii
abrakadana, 2016-05-31 12:40:06

Nested Yii2 pages?

Hello. I'm making a small blog in Yii2. I've been trying to make nested pages for a long time. Does not work.
Here is what I have:
urlManager router:
'page/<url:[\w_\/-]+>' => 'page/view',
Controller:

public function actionView($url)
{
  $explode = explode('/',$url);


  $post = Page::find()
    ->with(['page'])
    ->where([
      'page.url' => $url,
    ])
    ->one();
  if (!$post) {
    throw new NotFoundHttpException(Yii::t('page', 'The requested article does not exist.'));
  }
  return $this->render('view', [
    'post' => $post,
  ]);
}

Table:
page
id
name
parent_id
url
I want pages to be opened at addresses:
page/privet/
page/privet/toster
page/privet/toster/vopros-yii
Two points are not clear:
1) How to handle this in urlManager. Tried to do like this:
'page/<url:\w+>' 	=> 'page/view',
'page/<url>/<url2:\w+>' 	=> 'page/view',
'page/<url>/<url2>/<url3:\w+>' 	=> 'page/view',

Then like this:
<code>'page/<url:[\w_\/-]+>' 					=> 'page/view',</code>

I received the whole line and then did it in the explode controller.
2) How to make a request in the controller to display the page? e.g. page/privet/toster/vopros-yii. how to make a request by parent_id and url?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2016-05-31
@abrakadana

Good time of the day! You need to write your handler: www.yiiframework.com/doc-2.0/guide-runtime-routing.html here at the very end there is an example of a handler class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question