A
A
Alexander Nevsky2019-01-29 15:54:18
Yii
Alexander Nevsky, 2019-01-29 15:54:18

How to set up YII2 url rules properly?

I had a small problem, searched everything and seemed to understand that I need to do dynamic urls, but in general the essence is not clear how and what.
I wanted to make a CNC like this: https://site/Admin - for a profile, https://site/SaqhFe - for a book (SaqhFe - slug), https://site/SaqhFe/2 - for chapters (SaqhFe - slug, 2 - chapter number).
URLManger Code:

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            //'enableStrictParsing' => true,
            'rules' => [
                '/' => 'site/index',
                '<action:(login|signup|logout|news|contact)>' => 'site/<action>',

                'authorial' => 'authorial/index',
                'a/<action>' => 'authorial/<action>',

                'translation' => 'translation/index',
                't/<action>' => 'translation/<action>',

                [
                    'class' => 'yii\web\GroupUrlRule',
                    'routePrefix' => 'books',
                    'rules' => [
                        '<slug>' => 'view',
                        'create' => 'create',
                        '<slug>/settings' => 'update',
                    ],
                ],

                [
                    'class' => 'yii\web\GroupUrlRule',
                    'routePrefix' => 'profile',
                    'rules' => [
                        '<username>' => 'view',
                        '/profile/edit' => 'update',
                    ],
                ],

                'w/<action>' => 'wishlist/<action>',

                'c/settings' => 'chapter/settings',
                '<slug>/<number>' => 'chapter/view',
                '<slug>/<number>/editor' => 'chapter/update',
            ],
        ],

But a problem arises, when entering the book, it throws it on the account page (and of course it gives an error that there is no such account). The same thing when you go to a page like https://site/profile/edit, throws it on the chapters page, also with an error.
Please tell me how to solve this problem correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Verbitsky, 2019-01-30
@VerbAlexVlad

It's easier to write it like this:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    //'enableStrictParsing' => true,
    'rules' => [
        '/' => 'site/index',
        '<action:(login|signup|logout|news|contact)>' => 'site/<action>',

        'authorial' => 'authorial/index',
        'a/<action>' => 'authorial/<action>',

        'translation' => 'translation/index',
        't/<action>' => 'translation/<action>',

        'books/<slug:\d+>' => 'books/view',
        'books/create' => 'books/create', // Эту строку вообще не вижу смысла писать
        'books/<slug:\d+>/settings' => 'books/update',

        'profile/<username:\w+>' => 'profile/view',
        'profile/edit' => 'update',

        'w/<action>' => 'wishlist/<action>',

        'c/settings' => 'chapter/settings',
        '<slug:\d+>/<number:\d+>' => 'chapter/view',
        '<slug:\d+>/<number:\d+>/editor' => 'chapter/update',
    ],
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question