D
D
Dmitry2018-09-14 17:43:13
Yii
Dmitry, 2018-09-14 17:43:13

How to deal with slash at the end of URL in Yii2?

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'accounts'
                ],
                'OPTIONS accounts/<account_uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>' => 'accounts/view',
                'GET accounts/<account_uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>' => 'accounts/view',
                'PUT accounts/<account_uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>' => 'accounts/update',
                'POST accounts' => 'accounts/create',
                'OPTIONS accounts' => 'accounts/index',
                'GET accounts' => 'accounts/index',
            ]

At the front is Angular6, it pre-sends OPTIONS before each request
Yii correctly responds if
OPTIONS accounts/ 200
OPTIONS accounts 404
If the request is without a slash at the end, it will say 404
A POST request, on the contrary .. works correctly when there is no slash at the end of the URL
POST accounts / 404
POST accounts works correctly
Actually how to bring POST and OPTIONS requests to the same form (with or without a slash) .. so that they work at the same URL
PUT accounts/f5cf9bce-f12c-11e7-8f22-52540012f636 is also used - it has OPTIONS and PUT queries work the same way.. the confusion with the slash at the end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-14
@slo_nik

Good evening.
Try these settings.

'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'suffix' => '/',
      'normalizer' => [
          'class' => 'yii\web\UrlNormalizer',
          'normalizeTrailingSlash' => true,
          'collapseSlashes' => true,
       ],
// остальные настройки urlManager
]

Or just add only setting'suffix' => '/',

A
Andrey, 2018-09-14
@VladimirAndreev

that is, if requests are sent by the front, then everything works?
then what's the point of taking a steam bath due to the fact that something does not work for someone on the left?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question