Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Can you explain these two lines of code in applications/config/routes.php file in the CodeIgniter framework?

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
share|improve this question

1 Answer 1

up vote 6 down vote accepted

The following information is taken from the CodeIgniter-Documentation under http://codeigniter.com/user_guide/general/routing.html.

Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions

So your first line of code would redirect any call to your domain, when the URI contains no data to the view-Method in the pages-class like that: http://example.com/ would redirect to http://example.com/pages/view/

The second line would redirect any call to the pages-class/view-method and pass the original URI as parameter like that:

http://example.com/articles/ would redirect to http://example.com/pages/view/articles/

So, basically you're setting up a Front Controller with these two lines. Hope that helps.

EDIT:

In CodeIgniter, each URL can be splitted into class, method and data as stated here. Example: http://example.com/classname/methodname/somedata. Normally, calling the URL http:/example.com/show/1 would call the method 1 in the class article. Your second line of code would redirect this call to http://example.com/pages/view/articles/1 and therefor call the method view in the class pages passing the arguments "articles" and "1" as data.

I would like to encourage you to read through the excellent CI-Documentation to fully understand those principles.

share|improve this answer
    
can you please rephrase/elaborate part two(the second line)? –  Kumar Mar 30 '12 at 9:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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