简体   繁体   中英

What is routes.php in codeigniter

what is definitely the function of routes.php in codeigniter ?

My teacher was taught about it and said your views won't work if you don't use routes such as example:

$route['blabla'] = ['blabla/blabla']; and everything about it!

But for me, it's working without using any routes. I don't have idea why we should using routes. It's only waste time. Can anyone explain?

In codeigniter, PHP files are served in a different way rather than accessing the PHP files directly from the browser. This process is called as Routing .

Our code will work without rewriting the url in routes.php.

We all want to show our web page in a more convenient way, so that it can make more sense to visitors(also to search engines, of course). Such as, one should understand in brief what contents a page contains, just by checking out the URL in the browser address bar. If we keep this as it is in a way what can be understood by server scripts(PHP,Asp.NET etc), like as follows, shouldn't fulfill our goal much:

http://yourdomain.com?p=1
http://yourdomain.com?p=2
http://yourdomain.com?w=30
http://yourdomain.com?z=234
//etc......

From the above links, there is no way to understand what those pages are about. Now, what if it was something like as follows:

http://codesamplez.com/database/codeigniter-activerecord
http://codesamplez.com/programming/regular-expressions-in-php

These are much more meaningful, we can have a brief idea from just seeing the URL. Also, search engines gives some more value compared to the earlier urls, that's why they are known as 'search engine friendly URL'. So, whatever reason you choose, it's always better to use SEO friendly URL always. Ok, now, we have decided to make our site more SE/visitor friendly and want to use these urls. Now, how we should develop our application to map this urls to original request handler scripts?

Url Rewriting/Routing actually is the technique which converts this seo friendly urls to a format that server code can understand easily/drives a request to their corresponding request handler scripts.

CodeIgniter has user-friendly URI routing system, so that you can easily re-route URL. Typically, there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern −

your-domain.com/class/method/id/
  • The first segment represents the controller class that should be invoked.
  • The second segment represents the class function, or method, that should be called.
  • The third , and any additional segments, represent the ID and any variables that will be passed to the controller.

In some situations, you may want to change this default routing mechanism. CodeIgniter provides facility through which you can set your own routing rules.

There is a particular file where you can handle all these. The file is located at application/config/routes.php(you already knew that). You will find an array called $route in which you can customize your routing rules. The key in the $route array will decide what to route and the value will decide where to route. There are three reserved routes in CodeIgniter.

$route['default_controller']

This route indicates which controller class should be loaded, if the URI contains no data, which will be the case when people load your root URL. You are encouraged to have a default route otherwise a 404 page will appear, by default. We can set home page of website here so it will be loaded by default.

$route['404_override']

This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. It won't affect to the show_404() function, which will continue loading the default error_404.php file in application/views/errors/error_404.php.

$route['translate_uri_dashes']

As evident by the Boolean value, this is not exactly a route. This option enables you to automatically replace dashes ('-') with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. This is required because the dash is not a valid class or method-name character and will cause a fatal error, if you try to use it.

Watch this video for a basic idea

Refer this documentation too

In CI code will run without routes.php. routes.php are use to customise the url/routes like if you have url like

example.com/pages/about_us

where "pages" is your controller and "about_us" is method in that controller, if you want to show url without controller like example.com/about_us , then you need to use routes.php in this way

$route['about_us'] = ['pages/about_us'];

Routing in codeigniter one of major feature which is used for user friendly URL and SEO Friendly google search.

What does it mean: if any user searched for any product in your website so the URL will be www.domain.in/categalog/Deskstop-Intel-124343 Now in this case the url is user friendly how to interpret it is maintained in routing.

Every time the url is hitted control flow to routes and from routes To controller

$route['product/(:any)'] = 'catalog/product_lookup';

Now categlog/product_lookup is interpreted as product/any means there is one controller Product which accept Id/text as input.

This is as simple as making the user defined url as per our requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM