Yii framework: Controller/Action url & parameters
40,421
Solution 1
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');
Solution 2
Try This......
$id = Yii::app()->request->getParam('id');
Author by
Peterim
Updated on July 19, 2022Comments
-
Peterim almost 2 years
In my application , I have
ApiController
withactionUsers
, So in YII the path becomesapi/users
. Now in order to get certain users info , I use the following pathapi/users/id/10
where 10 is the userID andid
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 likeapi/users/10
?Thank you!
-
Matt Eskridge about 10 yearsIt's a shame that Yii doesn't do this mapping for you implicitly. For 95% of cases, /users/view/1/ should go to UserController::view($id) without you having to instruct Yii to make this connection.
-
Ankita almost 9 yearscan it be done like if user pass id then also it should go to the action and if not then also in the same action?