Skip to content

Instantly share code, notes, and snippets.

@pebriana
Forked from cebe/main.php
Created February 10, 2016 09:05
Show Gist options
  • Save pebriana/c4982929e156598eaf7b to your computer and use it in GitHub Desktop.
Save pebriana/c4982929e156598eaf7b 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>',
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment