문제

I'm developing a REST API. I found out a few extensions but the client need is drastically different from what these extensions provide.

I need a separate action for each of the request type in each controller, so I can have the full control to myself.

I'm currently using the following URLmanager rules;

'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

Which arranges my URL this way;

http://www.mywebsite.com/index.php/<module>/<controller>/<action>

This can easily service a request like this;

http://www.mywebsite.com/index.php/api/user/register

The problem I'm facing is how to service these type of requests.

http://www.mywebsite.com/index.php/api/user/15/profile

Is there a way I can get 15 (id/pk) and "profile" as two parameters?

Thanks in advance.

도움이 되었습니까?

해결책

You have to define it in urlmanager like so for e.g.:

'rules' => array(
    'api/user/<id:\d+>/<mode:\w+>' => 'api/user/someaction',
),

You can then go to http://www.mywebsite.com/index.php/api/user/15/profile and the call goes to api module, user controller, action someaction and passes the $_GET variables "id" and "mode" to it.

actionSomeaction($id, $mode)
{
    var_dump($id, $mode);
}

EDIT: to make it dynamic use this:

'rules' => array(
    '<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>/<mode:\w+>' => '<module>/<controller>/<action>',
),

However I would not make everything dynamic since you usually only want to pass the params id and mode to user/someaction.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top