V
V
Vocler2019-07-17 19:04:33
Yii
Vocler, 2019-07-17 19:04:33

Why does yii/helpers/Url::To generate a crooked URL during testing?

I display the URL generated by the function on the page yii/helpers/Url::To: If the entry point is a regular index.php (i.e. I go to the address ), then I see the correct URL: If the entry point is a test index-test.php (the address is ), then I see the following: Naturally, when when I click on the link, I get 404. the correct address must be . What is the reason for this behaviour? UPD: Apparently the bug appears only if used in the following way
<?= Url::to(['user/index']) ?>
http://localhost/
/user/index
http://localhost/index-test.php
/index-test.php/index-test.php/user/index
/index-test.php/user/index
Url::To()\yii\widgets\Menu::widget

\yii\widgets\Menu::widget(
            [
                'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
                'items' => [
                    ['label' => Yii::t('app', 'Label'), 'options' => ['class' => 'header']],
                    [
                        'label' => 'Users',
                        'url' => [Url::to(['user/index'])],
                    ],
        ]
      ]

The most interesting thing is that if you replace with (i.e. remove the square brackets around the edges and pass not an array with one line, but just one line to the url), everything works correctly, although the docks say that you need to pass an array. [Url::to(['user/index'])]Url::to(['user/index'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-07-17
@Vocler

You open the console.
Enter the command
As a result of this command, you should see something like this in the console.

Server started on http://localhost:8080/
Document root is "/home/slonik/localhost/www/test.loc/yii2_test/web"
Quit the server with CTRL-C or COMMAND-C.

Go to the address in your browser.
Do not change anything in the configuration files.
Getting page without 404 error.
ps
In config/web.php
'urlManager'   => [
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'rules'           => [
                ''                            => 'site/index',
                '<_a:(contact|about)>'        => 'site/<_a>',
                '<_a:(login|logout|sign-up)>' => 'users/<_a>',
                '<_c:[-\w]+>/<_a:[-\w]+>'     => '<_c>/<_a>',
                '<_c:[-\w]+>'                 => '<_c>/index',
                '<_a:(offline)>'              => 'offline/index',

            ],
        ],

In config/test.php
'urlManager' => [
            'showScriptName' => true,
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question