ali asad
ali asad

Reputation: 1349

what does $route['(:any)'] = 'pages/view/$1'; mean in Codeigniter Router

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';

Upvotes: 9

Views: 16657

Answers (2)

tomexsans
tomexsans

Reputation: 4527

$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

Upvotes: 25

dakdad
dakdad

Reputation: 2955

$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.

Upvotes: 9

Related Questions