Skip to content

Instantly share code, notes, and snippets.

@cebe
Last active June 21, 2019 09:25
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cebe/5674918 to your computer and use it in GitHub Desktop.
Save cebe/5674918 to your computer and use it in GitHub Desktop.
REST routing with Yii 2 UrlManager
<?php
return array(
/* ... */
'components' => array(
/* ... */
'urlManager' => array(
'enablePrettyUrl' => true,
'rules' => require(__DIR__ . '/routes.php'),
),
/* ... */
),
);
<?php
use yii\web\UrlRule;
return array(
// REST routes for CRUD operations
'POST <controller:\w+>s' => '<controller>/create', // 'mode' => UrlRule::PARSING_ONLY will be implicit here
'<controller:\w+>s' => '<controller>/index',
'PUT <controller:\w+>/<id:\d+>' => '<controller>/update' // 'mode' => UrlRule::PARSING_ONLY will be implicit here
'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete', // 'mode' => UrlRule::PARSING_ONLY will be implicit here
'<controller:\w+>/<id:\d+>' => '<controller>/view',
// normal routes for CRUD operations
'<controller:\w+>s/create' => '<controller>/create',
'<controller:\w+>/<id:\d+>/<action:update|delete>' => '<controller>/<action>',
);
@abhimanusharma
Copy link

Bug: In routes.php, a comma (,) is missing in line no. 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment