title: Yii2 REST API as Module route configuration
body: <p>I have an existing Yii2 application and have been trying to implement a REST API as an additional module (Maybe a module isn't the correct way to go about this?) But I'm having some trouble configuring the route structures. It doesn't quite work and doesn't follow the expected results, based of the <a href="http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html" rel="noreferrer">following guide</a>. </p>
  
  <p>I've built an additional module that looks like this:</p>
  
  <pre><code>module
    api
      controllers
        UserController.php
      Module.php
  </code></pre>
  
  <p><strong>UserController.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace app\modules\api\controllers;
  
  use yii\rest\ActiveController;
  
  class UserController extends ActiveController
  {
      public $modelClass = 'app\models\User';
  }
  </code></pre>
  
  <p><strong>Module.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace app\modules\api;
  
  /**
   * onco module definition class
   */
  class Module extends \yii\base\Module
  {
      public $defaultController = 'user';
      /**
       * @inheritdoc
       */
      public $controllerNamespace = 'app\modules\api\controllers';
  
      /**
       * @inheritdoc
       */
      public function init()
      {
          parent::init();
  
          // custom initialization code goes here
      }
  }
  </code></pre>
  
  <p>In my config file I have the added following:</p>
  
  <pre><code>'request' =&gt; [
      ...
              'parsers' =&gt; [
                  'application/json' =&gt; 'yii\web\JsonParser',
              ]
          ],
      ...
      'urlManager' =&gt; [
        'enablePrettyUrl' =&gt; true,
          'showScriptName' =&gt; false,
          'enableStrictParsing' =&gt; false, // have tried as true also
      'rules' =&gt; [
       ...
       ['class' =&gt; 'yii\rest\UrlRule', 'controller' =&gt; '\app\modules\api\controllers\user'],
              ],
       ],
       ...
       'modules' =&gt; [
        ...
        'api' =&gt; [ // module for RESTful API
              'class' =&gt; 'app\modules\api\Module',
          ],
      ]
  </code></pre>
  
  <p>When I run the following urls through postman I get the following:</p>
  
  <ul>
  <li><a href="http://localhost/site1/web/api/users" rel="noreferrer">http://localhost/site1/web/api/users</a> -> 404</li>
  <li><a href="http://localhost/site1/web/api/users/index" rel="noreferrer">http://localhost/site1/web/api/users/index</a> -> 404</li>
  <li><a href="http://localhost/site1/web/api/user/index" rel="noreferrer">http://localhost/site1/web/api/user/index</a> -> returns json repsonse</li>
  <li><a href="http://localhost/site1/web/api/user/2" rel="noreferrer">http://localhost/site1/web/api/user/2</a> -> 404</li>
  </ul>
  
  <p>I'm unsure as to why the predicted routes of noted in the docs as:</p>
  
  <blockquote>
    <p>Trying it Out With the above minimal amount of effort, you have
    already finished your task of creating the RESTful APIs for accessing
    the user data. The APIs you have created include: 
    GET /users: list all users page by page;</p>
    
    <p>HEAD /users: show the overview information of user listing; </p>
    
    <p>POST /users: create a new user;</p>
    
    <p>GET /users/123: return the details of the user 123;</p>
    
    <p>HEAD /users/123: show the overview information of user 123; </p>
    
    <p>PATCH /users/123 and PUT /users/123: update the user 123;</p>
    
    <p>DELETE /users/123: delete the user 123; </p>
    
    <p>OPTIONS /users: show the supported verbs regarding endpoint /users;</p>
    
    <p>OPTIONS /users/123: show the supported verbs regarding endpoint /users/123</p>
  </blockquote>
  
  <p>What have I likely done wrong in this setup? Is there a better way to implement an API into an existing website, whilst maintaining DRY practices? </p>