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!
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]);
They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.
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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With