REST API URL Rewrites with Module


(Scott) #1

In my config I have the following:


'modules' => [

            'v0' => [

                'class' => 'app\modules\v0\Module',

            ],

            'v1' => [

                'class' => 'app\modules\v1\Module',

            ],

        ],

And I have the following URL manager:


'urlManager' => [

            'enablePrettyUrl' => true,

            'enableStrictParsing'=>true,

            'showScriptName' => false,

            

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => ['v0/object']],

                ['class'=>'yii\rest\UrlRule','controller'=>['v0/user'],'extraPatterns'=>['POST authenticate'=>'authenticate']],

                ['class' => 'yii\rest\UrlRule', 'controller' => ['v1/object','v1/user']], 

            ],

        ],

My actions are defined as follows:


actionCreate($name)

actionView/Update/Delete ($name, $id)

My Urls currently look like this (and work):


https://myapi.com/v0/objects?name=warehouse

https://myapi.com/v0/objects/12345?name=warehouse

https://myapi.com/v0/objects/6789?name=warehouse

But I need them to look like this:


https://myapi.com/v0/objects/warehouse

https://myapi.com/v0/objects/warehouse/12345

I have looked into url rewriting but just can’t seem to figure out what I need to do / where it needs to go.

Could anyone shed some light on this please?

Thanks so much!