2

Trying to create API app (based on RESTFull services) using Yii2 Advanced template. Created subdomain api.domain.com for this purpose.

The directory structure looks like this:

enter image description here

The problem is, when I try to send GET request to api.domain.com/users getting following response:

{
  "name": "Not Found",
  "message": "Page not found.",
  "code": 0,
  "status": 404,
  "type": "yii\web\NotFoundHttpException",
  "previous": {
    "name": "Invalid Route",
    "message": "Unable to resolve the request \"user/index\".",
    "code": 0,
    "type": "yii\base\InvalidRouteException"
  }
}

Here is config file:

https://gist.github.com/d1930b6bf20e3d50fe63

Here is controller (which is located in Controllers folder):

https://gist.github.com/anonymous/180a7e791e879570e0f4

The question is...

what am I doing wrong?

Community
  • 1
  • 1
heron
  • 3,611
  • 25
  • 80
  • 148
  • what about your .htaccess file ? did you set extra server configs for clean urls support ? – Salem Ouerdani Aug 19 '15 at 10:11
  • @SalemOuerdani nginx file already configured. And it's in right route. – heron Aug 19 '15 at 10:51
  • not sure what is wrong but can you try to remove `components[response]` and the `\yii\web\Request` class from `components[request]` and try if it works ? you may check [this file](https://github.com/tunecino/Yii2_foundation-apps/blob/master/backend/config/api.php), my API works fine within those configs. – Salem Ouerdani Aug 19 '15 at 10:59
  • @SalemOuerdani response is ok. The error is not related to response – heron Aug 19 '15 at 11:09
  • Try with this url call `api.domain.com/index.php?r=users`, it works? In the .htacces you can hide the `index.php` part: http://stackoverflow.com/questions/26525320/enable-clean-url-in-yii2 –  Aug 19 '15 at 16:24

2 Answers2

5

Make sure that, you added api application's path into bootstrap.php in common folder. It must look like:

<?php
Yii::setAlias('common', dirname(__DIR__));
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');                    <-- this line

BTW, File located in common/config/bootstrap.php

Tural Ali
  • 22,202
  • 18
  • 80
  • 129
  • This fixed it for me, but my bootstrap.php was slightly different Yii::setAlias('@api', dirname(dirname(\_\_DIR\_\_)) . '/api'); – Nathan May 14 '18 at 18:34
0

3 things:

  1. Check pluralize. Ex: "v1/post" should be "v1/posts"
  2. Add "action" prefixe. Ex: "actionImport()"
  3. Kebab format. Ex: "v1/getSample" should be "v1/get-sample"
  4. Add rule 'extraPatterns' in main.php:urlManager if need be. Ex:

    ['class' => 'yii\rest\UrlRule', 'controller' => 'v1/report', 'extraPatterns'=>['analytics' => 'analytics', 'report' => 'report']],

Fractal Mind
  • 405
  • 4
  • 10