13

I am using the latest version of Yii and trying to basically collate the URL parameters.

For instance if my URL was as follows:

site.com/events/199/111

What is the function to grab the first parameter e.g '199' and similarly again to say grab the 2nd parameter e.g '111'.

I remember CodeIgniter has $this->url->segment(1), I basically need the same functionality to Yii :)

1
  • 2
    May be you simple to use CodeIgniter? Yii has a UrlManager, that uses rules in config. Rule like 'events/<param1:\d+>/<param2:\d+>' => 'events/show' call EventController->actionShow($param1,$param2) {} Commented Jan 23, 2014 at 13:27

4 Answers 4

19

On somthing like -

$this->createUrl('blah/blahx',array('id'=>2));

You can get your parameter using -

$x = CHttpRequest::getParam('id'); //outputs x=2 

OR $x = Yii::app()->getRequest()->getQuery('id'); //x=2 again

Sign up to request clarification or add additional context in comments.

2 Comments

I will do have to wait a few more mins :)
I had the follwing URL i.e. localhost/FedSeed/reports/multi/1 and I used CHttpRequest::getParam('id'); to access the last 1 digit :)
4

you can try getParam method

$id = Yii::app()->request->getParam('id');

1 Comment

The link is not working
1

You should read this :

About your problem, you should simply create a corresponding url rule, e.g. :

'events/<param1>/<param2>'=>'events/view',

And then in your controller :

public function actionView($param1, $param2)
{
    // you can now use $param1 and $param2
}

1 Comment

my url is like this bookings/booking/01-SEPT-2022 and I want to get the 01-SEPT-2022 part. 'extraPatterns' => [ 'POST booking/{j}-{M}-{Y}' => 'booking', 'POST bookingdet/{j}-{M}-{Y}' => 'bookingdet', ], . and then following your answer I have done public function actionBooking($j,$k,$l) after than when I do var_dump($k); it says "Missing required parameters: k, l",
-1

You just need uri component. Here is the solution , you can use uri component like this ; Yii::app()->uri->segment(2);

for details follow url

http://www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.