Answer the question
In order to leave comments, you need to log in
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',
],
],
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question