I have created a dynamic nav bar menu, but can't get the correct URL with a parameter.
Here's part of my model code:
public function getNavMenu($menu_id)
{
$menu = Menus::find($menu_id)
->where('menu_id = :menu_id', [':menu_id' => $menu_id])
->orderBy('order')
->all();
// $data=[];
foreach ($menu as $menus) {
if (isset($menus->parent)) {
$submenu[$menus->id] = [
'label' => Yii::t('frontend', ($menus['name'])),
'url' => parse_url($menus['route']),
'options' => [
['class' => 'nav nav-pills nav-stacked'],
['class' => 'nav nav-second-level'],
['class' => 'nav nav-third-level'],
],
];
$data[$menus->parent]['items'] = $submenu;
} else {
# menu
$data[$menus->id] = [
'label' => Yii::t('frontend', ($menus['name'])),
'url' => (substr($menus["route"], 0, 4) == "http")
? $menus["route"]
: [$menus["route"]],
];
}
}
return $data;
}
my page action :
public function actionView($slug)
{
$this->layout = '@frontend/views/layouts/original/main.php';
$model = Page::find()->where(['slug'=>$slug, 'status'=>Page::STATUS_PUBLISHED])->one();
if (!$model) {
throw new NotFoundHttpException(Yii::t('frontend', 'Page not found'));
}
$viewFile = $model->view ?: 'view';
return $this->render($viewFile, ['model'=>$model]);
}
My static route is: ['label' => Yii::t('frontend', 'About'), 'url' => ['/page/view', 'slug'=>'about']]
If I insert a record in the column link in one of the following ways:
/page/view, slug=>about
/page/about
The URL will display: http://yii2-web.dev/page/view,slug=about
or http://yii2-web.dev/page/about
and surely the result is not found.