Skip to content

Instantly share code, notes, and snippets.

@cebe

cebe/main.php

Last active Jun 21, 2019
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@abhimanusharma abhimanusharma commented Jul 22, 2018

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
You can’t perform that action at this time.