0

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.

sudo_ee_
  • 109
  • 5
  • 15

1 Answers1

0

Instead of build a custom routing you should use the routeing feature provided by yii2 using eg: Url::to(...)

Assuming the the value 'about' is inside $menus['route'], that your controller in named 'page' your action in named 'actionView' you shluld

use yii\helpers\Url;

// Url::to() calls UrlManager::createUrl() to create a URL
 $data[$menus->id]= ['label' => Yii::t('frontend', ($menus['name'])),
                       'url'=> Url::to(['/page/view', 'slug'=>$menus['route']])
              ];

try take a deep look at:

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

and

http://www.yiiframework.com/doc-2.0/guide-helper-url.html

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • i have tried with Url::to, still not found and displayed wrong url – sudo_ee_ Feb 27 '17 at 04:51
  • show me your controller and action name you want for the link you want route .. – ScaisEdge Feb 27 '17 at 07:10
  • i have update my question by adding page Controller(actionView). the correct static route in my layout is ['label' => Yii::t('frontend', 'About'), 'url' => ['/page/view', 'slug'=>'about']]. In My tabel record value is /page/view, slug=>about. about is name of page. – sudo_ee_ Feb 27 '17 at 08:14
  • Yuo mean that you have a controller named page and an action named about ? . what mean the view, slug =about ..? (seems not http get format) – ScaisEdge Feb 27 '17 at 08:19
  • i'm making dynamic page, the action named actionView. about is one of record page stored in table. To display record from table which about is slug from db i use the actionView. – sudo_ee_ Feb 28 '17 at 05:00