title: Yii2 Advanced Template - Rest API
body: <p>I've been following a few tutorials online but I can't seem to figure out how to create a REST API for Yii2. I keep getting a <code>404 page not found</code>. This is what I have so far:</p>
  
  <p>Folder structure: </p>
  
  <pre><code>api
  - config
  -- api.php
  -- bootstrap.php
  - modules
  -- v1
  --- controllers
  ---- UserController.php
  --- Api.php
  --- Module.php
  .htaccess
  index.php
  </code></pre>
  
  <p>api/api.php</p>
  
  <pre><code>&lt;?php
  
  $params = require(__DIR__ . '/params.php');
  
  $config = [
      'id' =&gt; 'api',
      'basePath'  =&gt; dirname(__DIR__).'/..',
      'bootstrap'  =&gt; ['log'],
  
      'components'  =&gt; [
          // URL Configuration for our API
          'urlManager'  =&gt; [
              'enablePrettyUrl'  =&gt; true,
              'showScriptName'  =&gt; false,
  
              'rules' =&gt; [
                  [
                      'class'  =&gt; 'yii\rest\UrlRule',
                      'controller'  =&gt; [
                          //'v1/user'
                          'users' =&gt; 'v1/users'
                      ],
                  ]
              ],
  
          ],
  
          'request' =&gt; [
              // Set Parser to JsonParser to accept Json in request
              'parsers' =&gt; [
                  'application/json'  =&gt; 'yii\web\JsonParser',
              ]
          ],
  
          'cache'  =&gt; [
              'class'  =&gt; 'yii\caching\FileCache',
          ],
  
          // Set this enable authentication in our API
          'user' =&gt; [
              'identityClass'  =&gt; 'app\models\User',
              'enableAutoLogin'  =&gt; false, // Don't forget to set Auto login to false
          ],
  
          // Enable logging for API in a api Directory different than web directory
          'log' =&gt; [
              'traceLevel'  =&gt; YII_DEBUG ? 3 : 0,
              'targets'  =&gt; [
                  [
                      'class'  =&gt; 'yii\log\FileTarget',
                      'levels'  =&gt; ['error', 'warning'],
                      // maintain api logs in api directory
                      'logFile'  =&gt; '@api/runtime/logs/error.log'
                  ],
              ],
          ],
  
          'db'  =&gt; require(__DIR__ . '/../../config/db.php'),
      ],
  
      'modules' =&gt; [
          'v1' =&gt; [
              'basePath' =&gt; '@api/modules/v1',
          'class' =&gt; 'api\modules\v1\Api',
          ]
      ],
  
      'params'  =&gt; $params,
  ];
  
  return $config;
  </code></pre>
  
  <p>api/bootstrap.php</p>
  
  <pre><code>&lt;?php
      Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
  </code></pre>
  
  <p>api/modules/v1/controllers/UserController.php</p>
  
  <pre><code>&lt;?php
  
  namespace app\api\modules\v1\controllers;
  
  class UserController extends ActiveController
  {
  
      public $modelClass = 'app\models\User';
  }
  </code></pre>
  
  <p>api/modules/v1/Api.php</p>
  
  <pre><code>&lt;?php
  
  namespace api\modules\v1;
  
  use \yii\base\Module;
  
  class Api extends Module
  {
      public $controllerNamespace = 'api\modules\v1\controllers';
  
      public function init()
      {
          parent::init();
  
          // custom initialization code goes here
      }
  }
  </code></pre>
  
  <p>api/modules/v1/Module.php</p>
  
  <pre><code>&lt;?php
  
  namespace api\modules\v1;
  
  class Module extends \yii\base\Module
  {
      public $controllerNamespace = 'api\modules\v1\controllers';
  
      public function init()
      {
          parent::init();
          \Yii::$app-&gt;user-&gt;enableSession = false;
          // custom initialization code goes here
      }
  }
  </code></pre>
  
  <p>api/.htaccess</p>
  
  <pre><code>Options +FollowSymLinks
  IndexIgnore */*
  
  RewriteEngine on
  
  # if a directory or a file exists, use it directly
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  
  # otherwise forward it to index.php
  RewriteRule . index.php
  </code></pre>
  
  <p>api/index.php</p>
  
  <pre><code>&lt;?php
  
  defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_ENV') or define('YII_ENV', 'dev');
  
  require(__DIR__ . '/../vendor/autoload.php');
  require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
  
  require(__DIR__ . '/config/bootstrap.php');
  
  $config = require(__DIR__ . '/config/api.php');
  
  $application = new yii\web\Application($config);
  
  $application-&gt;run();
  </code></pre>
  
  <p><code>mod_rewrite</code> is enabled</p>
  
  <p>Not sure what else needs to be done from here. I also have this added to <code>common/config/main.php</code> </p>
  
  <pre><code>'modules' =&gt; [
  
      'user' =&gt; [
          'class' =&gt; 'dektrium\user\Module',            
      ],
  
      'v1' =&gt; [
          'basePath' =&gt; '@api/modules/v1',
          'class' =&gt; 'api\modules\v1\Api',
      ]
  
  ],
  </code></pre>
  
  <p>I use Postman to send GET requests: 
  <code>http://example.com/api/v1/users</code></p>
  
  <p>and </p>
  
  <p><code>http://example.com/v1/users</code> which returns <code>Class app\api\modules\v1\Api does not exist</code></p>
  
  <p>I made a few changes to the configuration and files, but no luck. This is the cleanest version I have.</p>