CodeIgniter framework URL routing summary, codeigniter framework url

Source: Internet
Author: User
Tags codeigniter

CodeIgniter framework URL routing summary, codeigniter framework url

URI Routing

Generally, a URI string has a unique controller class/method. Each part of URI is in the following pattern ):
Copy codeThe Code is as follows:
Example.com/class/function/id/

However, in some examples, you may want to redirect this relationship to call a different class/function instead of a one-to-one response to the URL ).

For example, you may want your URL to adopt the prototype ):
Copy codeThe Code is as follows:
Example.com/product/1/
Example.com/product/2/
Example.com/product/3/
Example.com/product/4/

In general, the second part of the URL represents the method name, but in the above example, it represents the ID of a product. CodeIgniter can implement this function, allowing users to redirect (remap) URI handlers.

Set your own routing rules

The routing rules are defined in the application/config/routes. php file. In this file, you can see an array named $ route, which allows you to define your own routing rules. The following two methods can be used for definition: wildcard (wildcards) or Regular expression (Regular Expressions)

Wildcard

A typical wildcard routing looks like this:
Copy codeThe Code is as follows:
$ Route ['product/(: num) '] = "catalog/product_lookup ";

In a route, the keys of the array contain the matched URI, and the values of the array contain the destination to which the route will be redirected. in the preceding example, if the word "product" appears in the first part of the URL and the number (: num) appears in the second part of the URI, the "catalog" class and "product_lookup" method will be used in place (to be redirected soon ).

You can match the text value or use the following two wildcard types:

: Num will match a segment (segment) that only contains numbers ).

: Any will match any character (it can be multiple segment segments). It can match multiple values, such:

$ Route ['product/(: any) '] = "catalog/product_lookup/$1/$2/$3/$4/$5 "; // pass all parameters in the entire url to the product_lookup method under the catalog controller.

Note: The routes will run in the defined order. High-level routes always take precedence over low-level routes.

Example

Below are some simple examples:

Copy codeThe Code is as follows:
$ Route ['journ'] = "blogs ";

If the first segment (class name) of the URL is the keyword "journals", it will be redirected to the "blogs" class for processing.

Copy codeThe Code is as follows:
$ Route ['blog/job'] = "blogs/users/34 ";

If the first two segments of the URL are "blog" and "joe", they will be redirected to the "users" method of the "blogs" class for processing. and set ID "34" as the parameter.

Copy codeThe Code is as follows:
$ Route ['product/(: any) '] = "catalog/product_lookup ";

When "product" is the first segment in the URL, whatever the second segment is, it will be redirected to the "product_lookup" method of the "catalog" class.

Copy codeThe Code is as follows:
$ Route ['product/(: num) '] = "catalog/product_lookup_by_id/$1 ";

When "product" is the first segment in the URL, if the second segment is a number, it will be redirected to the "catalog" class, and pass the matched content to the "product_lookup_by_id" method.

Important: do not add "/" to the front or back "/".

Regular Expression

If you like, you can use regular expressions to customize your routing rules. Any valid regular expressions are allowed, or even reverse references.

Note: If you use reverse references, replace the double backslash syntax with the dollar symbol syntax (\ 1 with $1 ).

A typical regular expression looks like the following:
Copy codeThe Code is as follows:
$ Route ['products/([a-z] +)/(\ d +) '] = "$1/id _ $2 ";

In the preceding example, the URI similar to products/shirts/123 is changed to the id_123 method of the shirts controller class.

You can also mix wildcards with regular expressions.

Routes retained by the System

The system retains two routes:

The first is the default route:

Copy codeThe Code is as follows:
$ Route ['default _ controller'] = 'Welcome ';

This route indicates the Controller to be loaded when the URI does not contain the class and controller information to be accessed (that is, only accessing the root directory, such as http: // localhost/ci. In the preceding example, the system loads the "welcome" Class (Controller ). Make sure to set a default route. Otherwise, your homepage will display the 404 error.

The second route for the 404 page:
Copy codeThe Code is as follows:
$ Route ['2014 _ override'] = '';

This route identifies the Controller to be loaded if the requested controller cannot be accessed. It overwrites the default 404 error page (that is, it provides a custom 404 page function ). But it does not affect the show_404 () method. This method will still load the default error_404.php page in application/errors/error_404.php.

Important: The reserved route should be defined before all wildcard or regular expression routes.


How to obtain the URL of the current page in the PHP CodeIgniter framework

// Use url helper
$ This-> load-> helper ('url ');
Current_url = current_url ();
 
How can I delete the indexphp, instance in the URL segment of the CodeIgniter framework? I can see from the manual that I wrote a dome test.

To achieve this, we can only use pseudo-static .. Htaccess configuration rule implementation
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.