SaidbakR
SaidbakR

Reputation: 13552

Yii2 Bootstrap navbar's label URL without clean URL

I still under development and I don't want to use clean URLs in my Yii2 right now.

I added the following code to the advanced Yii2 project navbar About to make submenu under About:

[
    'label' => 'About',
    'url' => ['/site/about'],
    'items' => [
        ['label' => 'Invoices', 'url' => 'invoices/index'],
    ]
],

Normally, the invoices controller is accessed by: /?r=invoices from the client's address bar. However, the above code generates /invoices/index which gives Page not found error because I did not enabled clean URLs yet.

My question is, how could I add, accessible, new menu items as regarded above without clean URLs enabled while keeping the code as it is after enabling clean URLs?

Upvotes: 2

Views: 962

Answers (1)

arogachev
arogachev

Reputation: 33548

url parameter value is processed by \yii\helpers\Url::to() method:

  • false (default): generating a relative URL.
  • true: returning an absolute base URL whose scheme is the same as that in \yii\web\UrlManager::hostInfo.
  • string: generating an absolute URL with the specified scheme (either http or https).

So if you pass a string to url parameter, it will be treated as absolute url. In order to create url for given route, you need to pass array, then it will be processed by \yii\helpers\Url::toRoute():

'url' => ['invoices/index'],

It will consider UrlManager settings, such as $enablePrettyUrl.

Upvotes: 4

Related Questions