Yii 2 Rest Service Url Management

Hi,

I need some help with my Yii2 config.

I just want to offer a Rest api for my Yii 2 application. I saw the guide and in this default case everything is clear.

But I want some “tricky” path and url things and I don’t know how to achieve them.

First thing, my api controllers are outside my web root, like this:

  • "Complete Application folder"

---- "Api"

-------- "controllers"

---- "Web root"

-------- "assets"

-------- "components"

-------- "config"

-------- "…"

Second thing is, I want my urls under the prefix "Api", like this: hp://*.myapp.de/Api/Model

Can someone help me to achieve this?

Kind regards

No answer yet ;(

Is it not possible?

Hello SharpNoiZy, how are you?

I think (just think, i’m not sure) that the best way is with a module. You need to create a Api module, and after that treat the url managment to point to this module.

Like this:




[

    'PUT,PATCH api/model/<id>' => 'api/model/update',

    'DELETE api/model/<id>' => 'api/model/delete',

    'GET,HEAD api/model/<id>' => 'api/model/view',

    'POST api/model' => 'api/model/create',

    'GET,HEAD api/model' => 'api/model/index',

],



Remember that your ModelController needs to extends yii\rest\Controller.