Implement Restful Web Service In Modules

Hi All,

I would liked to implement the RESTful Web Service APIs in my module but I can’t find any explanations on how to do this.

Anyone has been able to do this yet ?

Thanks for your help.

Dave747

Create a usual module in your frontend with name "api".

Then enable it in main config:




    'modules' => [

        'api' => [

            'class' => 'frontend\modules\api\Api',

        ],

    ],



And add routes in "components" section in main config:




        'urlManager'=>[

            'class'=>'yii\web\UrlManager',

            'enablePrettyUrl'=>true,

            'showScriptName'=>false,


            /*

             * REST api rules

             */

            'rules' => [

                [

                    'class' => 'yii\rest\UrlRule',

                    'controller' => 'api/user',

                    'except' => [

                        'delete',

                    ],

                    'extraPatterns' => [

                        'POST login' => 'login',

                        'POST logout' => 'logout',

                        'POST forgot' => 'forgot',

                    ],

                ],

            ],

        ],



Hi Alex-ks,

Thank you very much for your help. Its gave me some directions but I’m using the basic application template, so I don’t have the frontend directory.

To be more precise, what I want to do is creating a bunch of modules, each one correspond to a specific functionality of my application, i.e.

  • purchase module

  • management module

For each module, I would liked to expose the following RESTful APIs:

  • GET /purchase

  • GET /management

  • POST /purchase

  • POST /management

After creating these modules, how do I manage to access the resources via the following urls:

  • localhost/api/purchase

  • localhost/api/management

Thanks

Dave

You need to read the Guide. One comment though, version your API to cater for future changes without killing old api quickly. I mean name them something like


localhost/api/v1/purchase

See:

http://stuff.cebe.cc/yii2docs/guide-rest-quick-start.html

Maybe it’s better to use 1 API module with controllers: purchase, management? Or you have a lot of functionality?

Thanks for your help.

I’ve already read the Guide but can’t find any suitable information about what I want to do.

Dave

which ones are missing?

The guide explained how to implement the RestFul API in the application itself but not in modules.

Sorry for my late reply, I was limited to 3 posts only as I’m new to the forum.

Sure, that’s the reason I want to divide my application into modules.

I’ve managed to get it to to work but only the GET method and the response format is XML and I am unable to redefine it to JSON.

Am I supposed to define all the 4 functions in the DefaultController of the module ?

What is the purpose of the class extending \yii\base\Module when generating a module ?

Unfortunately, there not much documentation on implementing the RestFul API in module for Yii2.

Thanks again.

Whats the difference? Module is application within applications.

Module can have its models/controllers/views yet share apps’

So the same applies to module!

Formatting Response Data

By default, Yii supports two response formats for RESTful APIs: JSON and XML. If you want to support other formats, you should configure the contentNegotiator behavior in your REST controller classes as follows,


  

use yii\helpers\ArrayHelper;


public function behaviors()

{

return ArrayHelper::merge(parent::behaviors(), [

	'contentNegotiator' => [

    	'formats' => [

        	// ... other supported formats ...

    	],

	],

]);

}

Yes if that is the RESt controller!

That is module file where you initialize stuffs in there!

As I said in other post, there is no significant difference and that is the reason!

Ok, I followed the guide on implementing the RestFul API, section [b]API Versioning

[/b]In the modules folder, I have the following structure:

My configuration settings is as follows:

Controller Class:

And finally, the results:

I can’t figure out what is wrong.

Your controller in the screeenshot is named ManagementController and in your browser address bar you are typing it wrongly as managements.

Try with:


http://127.0.0.1/basic/v1/management

Nope, same error page.

Does anybody have any idea how to resolve this issue. Its been 5 days I’m stucked on this.

Thanks very much.

Dave,

Reading the Guide seems to suggest that api version be module name. That is, instead of module name to be "api" it should go something like v1 or v2. See here

1 Like

I have not attempted to implement rest with yii, but I was curious from a learning standpoint. The Versioning docs don’t make sense to me. If api is a module as suggested in the docs, then v1 and v2 should be treated as nested modules. If this is the case, then the directory structure should be:


modules/

    api/

        controllers/

        models/

        modules/

            v1/

                controllers/

                models/

                views/

            v2/

                controllers/

                models/

                views/

        views/

Assuming api is a module, I would think that the config should be something like this:


'modules' => [

    'api' => [

        'class' => '@app/modules/api/Api',

        'modules' => [

            'v1' => [

                'class' => '@app/modules/api/modules/v1/V1',

            ],

            'v2' => [

                'class' => '@app/modules/api/modules/v2/V2',

            ],

        ],

    ],

],

If you look at the todo list for the docs, numerous documents have not been written yet. This includes module documentation. A lot of effort is also being spent on adding cross references between documents and adding more example code. As with the 1.x docs, I expect to see numerous clarifications and corrections as time passes.

I’m having the same error like Dave. Anyone have the solutions ?

Dave and others - this is not related to Yii REST API… but probably could help. The way in which you register modules in Yii config is important.

If you are configuring a module in Yii - you need to typically pass a class for the module in your configuration (this class should extend from \yii\base\Module).

So in your example where you are registering the module in your configuration, instead of just setting the basePath - you need to do this:




'modules' => [

   'v1' => [

       'class' => 'app\modules\api\v1\Module',  // the namespaced Module class

   ]

]



and your module class (Module.php) in app\modules\api\v1\




namespace app\modules\api\v1;

class Module extends \yii\base\Module

{

   public function init()

   {

       parent::init();

       // any other code

   }

}



AFAIK your premise is wrong. Docs suggests that API be a separate Application. So that should make sense.

I was advising with the OP directory structure in mind.