The simplest usage of UrlRule is to declare a rule like the following in the application configuration, php [ 'class' => 'yii\rest\UrlRule', 'controller' => 'user', ] The above code will create a whole set of URL rules supporting the following RESTful API endpoints: - 'PUT,PATCH users/' => 'user/update': update a user - 'DELETE users/' => 'user/delete': delete a user - 'GET,HEAD users/' => 'user/view': return the details/overview/options of a user - 'POST users' => 'user/create': create a new user - 'GET,HEAD users' => 'user/index': return a list/overview/options of users - 'users/' => 'user/options': process all unhandled verbs of a user - 'users' => 'user/options': process all unhandled verbs of user collection You may configure [[only]] and/or [[except]] to disable some of the above rules. You may configure [[patterns]] to completely redefine your own list of rules. You may configure [[controller]] with multiple controller IDs to generate rules for all these controllers. For example, the following code will disable the delete rule and generate rules for both user and post controllers: php [ 'class' => 'yii\rest\UrlRule', 'controller' => ['user', 'post'], 'except' => ['delete'], ] The property [[controller]] is required and should represent one or multiple controller IDs. Each controller ID should be prefixed with the module ID if the controller is within a module. The controller ID used in the pattern will be automatically pluralized (e.g. user becomes users as shown in the above examples). For more details and usage information on UrlRule, see the guide article on rest routing.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\web\CompositeUrlRule
コード例 #1
0
ファイル: UrlRule.php プロジェクト: luyadev/luya-module-admin
 public function init()
 {
     $map = Yii::$app->getModule('admin')->controllerMap;
     if (count($map) > 0) {
         foreach ($map as $alias => $className) {
             $class = sprintf('%s/%s', 'admin', $alias);
             $this->controller[] = $class;
         }
         parent::init();
     } else {
         $this->controller = [];
     }
 }
コード例 #2
0
ファイル: UrlRuleTest.php プロジェクト: howq/yii2
 public function testParseRequest()
 {
     $manager = new UrlManager(['cache' => null]);
     $request = new Request(['hostInfo' => 'http://en.example.com', 'methodParam' => '_METHOD']);
     $suites = $this->getTestsForParseRequest();
     foreach ($suites as $i => $suite) {
         list($name, $config, $tests) = $suite;
         $rule = new UrlRule($config);
         foreach ($tests as $j => $test) {
             $request->pathInfo = $test[0];
             $route = $test[1];
             $params = isset($test[2]) ? $test[2] : [];
             $_POST['_METHOD'] = isset($test[3]) ? $test[3] : 'GET';
             $result = $rule->parseRequest($manager, $request);
             if ($route === false) {
                 $this->assertFalse($result, "Test#{$i}-{$j}: {$name}");
             } else {
                 $this->assertEquals([$route, $params], $result, "Test#{$i}-{$j}: {$name}");
             }
         }
     }
 }
コード例 #3
0
ファイル: UrlRule.php プロジェクト: ancor-dev/yii2-rest
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->patterns = ['PATCH' => 'update-collection'] + $this->patterns;
     parent::init();
 }
コード例 #4
0
ファイル: UrlRule.php プロジェクト: psesd/cascade-lib
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->tokens['{id}'] = '<id:\\S+>';
     $this->tokens['{type}'] = '<type:\\S+>';
     parent::init();
 }