Geeth Welagedara Geeth Welagedara - 2 months ago 15
PHP Question

Codeigniter Regex for URL routing Issue

I have function call

abc
in
main
controller.

public function abc($value = NULL)
{
if ($value == 10) {
echo "value is 10";
} else {
echo "value is not TEN";
}
}


When i used URL route for function
abc
as following

http://localhost/males/7

$route['(males|female)'] = 'main/abc'; //working
$route['(males|female)/([0-9]+)'] = 'main/abc/$1'; //not working


But when i used separately it works. why?

$route['(males|female)'] = 'main/abc';
$route['males/([1-7]+)'] = 'main/abc/$1';
$route['female/([1-7]+)'] = 'main/abc/$1';

Answer

try using $2

$route['(males|female)/([0-9]+)']   = 'main/abc/$2';

i believe $1 is matching on (males|female) not the digits

it works in your second case as you have dropped the first capture group and $1 is now referencing what you want.

Comments