Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 REST API as Module route configuration

Tags:

rest

php

api

yii2

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 following guide.

I've built an additional module that looks like this:

module
  api
    controllers
      UserController.php
    Module.php

UserController.php

<?php

namespace app\modules\api\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}

Module.php

<?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
    }
}

In my config file I have the added following:

'request' => [
    ...
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
    ...
    'urlManager' => [
      'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false, // have tried as true also
    'rules' => [
     ...
     ['class' => 'yii\rest\UrlRule', 'controller' => '\app\modules\api\controllers\user'],
            ],
     ],
     ...
     'modules' => [
      ...
      'api' => [ // module for RESTful API
            'class' => 'app\modules\api\Module',
        ],
    ]

When I run the following urls through postman I get the following:

  • http://localhost/site1/web/api/users -> 404
  • http://localhost/site1/web/api/users/index -> 404
  • http://localhost/site1/web/api/user/index -> returns json repsonse
  • http://localhost/site1/web/api/user/2 -> 404

I'm unsure as to why the predicted routes of noted in the docs as:

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;

HEAD /users: show the overview information of user listing;

POST /users: create a new user;

GET /users/123: return the details of the user 123;

HEAD /users/123: show the overview information of user 123;

PATCH /users/123 and PUT /users/123: update the user 123;

DELETE /users/123: delete the user 123;

OPTIONS /users: show the supported verbs regarding endpoint /users;

OPTIONS /users/123: show the supported verbs regarding endpoint /users/123

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?

like image 253
Jonnny Avatar asked Feb 14 '17 15:02

Jonnny


People also ask

How do I create a REST API in yii2 basic?

<? php namespace app\controllers; use yii\rest\ActiveController; class UserController extends ActiveController { public $modelClass = 'app\models\User'; } ?> How do you access regular controllers?


1 Answers

try this:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
         [
            'class' => 'yii\rest\UrlRule', 
            'controller' => ['api/user'],
         ]
    ]
],
...
'modules' => [
  ...
  'api' => [
        'basePath' => '@app/modules/api',
        'class' => 'app\modules\api\Module',
    ],
]

Also be sure to implement prettyUrl's related server server configs.

like image 113
Salem Ouerdani Avatar answered Sep 20 '22 01:09

Salem Ouerdani