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';
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.
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.
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.
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With