heron
heron

Reputation: 3659

YII2 Restfull strange behaviour

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?

Upvotes: 2

Views: 934

Answers (2)

Fractal Mind
Fractal Mind

Reputation: 462

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']],

Upvotes: 0

Tural Ali
Tural Ali

Reputation: 23290

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

Upvotes: 5

Related Questions