9

In documentation of Codeigniter https://www.codeigniter.com/user_guide/tutorial/static_pages.html

I could not get what does $1 mean in expression $route['(:any)'] = 'pages/view/$1';

1
  • 2
    You use braces to catch a matching result, every pair of braces have a number so if you have (something)(another) the first pair is $1, the other is $2 and so on.
    – Frank
    Jan 28, 2013 at 19:05

2 Answers 2

25

$route['(:any)'] = 'pages/view/$1'; means that anything you type on the url will proceed to pages/view/$1 the $1 here is the parameter you would like to pass to a controller/method example

$route['login/(:any)'] = 'home/bacon/$1';

in this example you are telling CI that anything that goes to login with any parameter like login/john will proceed to your home/bacon/john (:any) will match all string and integer if you use (:num) it will only match integer parameters like

$route['login/(':num')'] = 'home/bacon/$1'

in this config you are specifying that if a url login has a integer after it like login/1234, you would like it to redirect to home/bacon/1234 if you don't know how many parameters you would like to pass you could try $route['login/(:any).*'] = 'home/bacon/$1' more on this could be read at https://www.codeigniter.com/user_guide/general/routing.html

9

$1 would be whatever matched by (:any) group - which is, really, anything. Whatever you add will get passed as the parameter for view method in pages controller.

More here on routing with codeigniter.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.