Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii framework: Controller/Action url & parameters

Tags:

url

path

php

yii

In my application , I have ApiController with actionUsers, So in YII the path becomes api/users. Now in order to get certain users info , I use the following path api/users/id/10 where 10 is the userID and id part of the path is basically a GET parameter (api/users?id=10).

Is there any way to do the same thing without id part of the path, i.e. I want my path to look like api/users/10?

Thank you!

like image 555
Peterim Avatar asked May 03 '10 20:05

Peterim


People also ask

How to create URL in yii?

For example, you can use the following code to create a URL for the post/view action: use yii\helpers\Url; // Url::to() calls UrlManager::createUrl() to create a URL $url = Url::to(['post/view', 'id' => 100]);

What is controller in Yii?

They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.

What is model Yii?

Yii implements two kinds of models: Form models and active records. They both extend from the same base class, CModel. A form model is an instance of CFormModel. Form models are used to store data collected from user input. Such data is often collected, used and then discarded.

What is composer in Yii?

Composer is a tool for dependency management in PHP. Yii2 uses it to install itself and other vendors' modules (for example, bootstrap). It is also possible to install Yii2 in the old way, by downloading the complete package and transferring it to the host, local or remote, where the framework will be installed.


1 Answers

You're going to need to put in rule patterns in the urlManager component:

Yii Framework Documentation: url

Your config should look something like this:

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'api/users/<id>'=>'api/users',
            ),
        ),
    ),
);

You can then get the value by:

$id = Yii::app()->getRequest()->getQuery('id');
like image 58
Shiki Avatar answered Sep 22 '22 12:09

Shiki