Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

codeigniter

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

like image 864
ali asad Avatar asked Jan 28 '13 16:01

ali asad


People also ask

What do you mean by routing in CodeIgniter?

Routing – routing is responsible for responding to URL requests. CodeIgniter Routing matches the URL to the pre-defined routes. If not route match is found then CodeIgniter throws a page not found exception. Controllers – routes are linked to controllers. Controllers glue the models and views together.

Why is there a need to configure the URL routes in CodeIgniter?

URLs in CodeIgniter are designed to be short and search engine friendly. It should make more sense to the visitors. A user should get an idea about the page content through its URL.

How do you call a route in CI?

You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config. php ), for example : $config['routes']['insertUser'] = 'users/add'; then supply the config above into the standard routes item on routes.


1 Answers

$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

like image 164
tomexsans Avatar answered Nov 10 '22 04:11

tomexsans