sparkmix
sparkmix

Reputation: 2489

PHP codeigniter - multiple parameters in routes?

I've this in my config/routes.php:

$route['music/artist/(:any)'] = "music/artist/index/$1";

I've an artist controller inside controller/music/ and index method.

So if I go to browser with domain.com/music/artist/michael then with the following line in controller I can get the data michael.

$this->uri->segment(3);

Please correct me if I'm wrong but here's the real question.

Now I want to have this on link.

domain.com/music/artist/michael/video/beat-it/

I want to have a video function inside the artist controller and inside I want to get the dynamic data, michael and beat-it.

So the routes config I've these:

$route['music/artist/(:any)'] = "music/artist/index/$1";
$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";

If I go to the video link it seems it just hit the index function.

How can I make the video link work?

Upvotes: 1

Views: 1658

Answers (1)

Tpojka
Tpojka

Reputation: 7111

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

Switch the places of routes. Most specific routes need to be parsed first because (:any) placeholder would take anything there.

$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";
$route['music/artist/(:any)'] = "music/artist/index/$1"; 

Reference.

Upvotes: 1

Related Questions