CodeIgniter Framework URL Routing Summary, codeigniter framework url_php Tutorial

Source: Internet
Author: User
Tags codeigniter

CodeIgniter Framework URL Routing summary, CodeIgniter framework URL


URI Routing

In general, a URI string has a controller class/method that corresponds to it. The various parts of the URI are as follows (pattern):
Copy the Code code as follows:
Example.com/class/function/id/

In some cases, however, you might want to redirect this relationship to invoke a different class/method (Class/function) instead of the one that corresponds to URL one by one.

For example, you might want to make your URL use this prototype (prototype):
Copy the Code code 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 example above, it represents the ID of a product. CodeIgniter can implement this feature so that the user can redirect (remap) the URI handler.

Set your own routing rules

Routing rules are defined in the application/config/routes.php file. In this file, you can see an array named $route that allows you to define your own routing rules. Definitions can be in two ways: wildcards (wildcards) or regular expressions (Regular Expressions)

Wildcard characters

A typical wildcard route looks like this:
Copy the Code code as follows:
$route [' product/(: num) '] = "catalog/product_lookup";

In a route, the key of the array contains the URI that is matched, and the value of the array contains the destination where the route will be redirected. In the example above, if the word "product" appears in the first part of the URL, and the number (: num) appears in the second part of the URI, "catalog" Class and the "Product_lookup" method will be substituted (soon to be redirected).

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

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

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

$route [' product/(: any) '] = "catalog/product_lookup/$1/$2/$3/$4/$5"; Pass each parameter on the entire URL to the Product_lookup method under the catalog controller.

Note: Routes will run in the order they are defined. High-level routing always takes precedence over lower-layer routes.

Example

Here are some simple examples:

Copy the Code code as follows:
$route [' journals '] = "blogs";

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

Copy the Code code as follows:
$route [' blog/joe '] = "BLOGS/USERS/34";

If the first two segments of the URL are "blog" and "Joe", they are redirected to the "users" method in the "Blogs" class. and set the ID "34" as a parameter.

Copy the Code code as follows:
$route [' product/(: any) '] = "catalog/product_lookup";

When "Product" is the first fragment in a URL, whatever the second fragment is, it is redirected to the "Product_lookup" method of the "Catalog" class.

Copy the Code code as follows:
$route [' product/(: num) '] = "catalog/product_lookup_by_id/$1";

When "Product" is the first fragment in the URL, if the second segment is a number, it is redirected to the "catalog" class and the matching content is passed to the "product_lookup_by_id" method.

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

Regular expressions

If you like, you can use regular expressions to customize your routing rules. Any valid regular expression is allowed, even a reverse reference.

Note: If you use a reverse reference, replace the double backslash syntax with the dollar character syntax (\\1 is replaced by $).

A typical regular expression looks like this:
Copy the Code code as follows:
$route [' products/([a-z]+]/(\d+) '] = "$1/id_$2";

In the above example, a URI similar to products/shirts/123 will be replaced with the Id_123 method that calls the shirts controller class.

You can also mix wildcard characters with regular expressions.

System-Reserved routes

The system retains two routes:

The first one is the system default route:

Copy the Code code as follows:
$route [' default_controller '] = ' welcome ';

This route indicates which controller will be loaded when the URI does not contain the class and controller information to access (that is, only the root directory, such as HTTP://LOCALHOST/CI). In the example above, the system will load the class "Welcome" (Controller). You should make sure to set a default route, otherwise your home page will display a 404 error.

The second route for a 404 page:
Copy the Code code as follows:
$route [' 404_override '] = ';

This route identifies which controller will be loaded if the requested controller is unreachable. It is equivalent to overwriting the default 404 error page (that is, providing the ability to define 404 pages yourself). However, it does not affect the show_404 () method, which will still load the default error_404.php page in application/errors/error_404.php.

Important: Reserved routes should be defined before all wildcard characters or regular expression routes are routed.


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

Use URL Helper
$this->load->helper (' url ');
Current_url = Current_url ();

How to delete the CodeIgniter frame in the URL segment inside the indexphp, instance, the manual on the wood to see clearly, I wrote a dome test is not

To achieve this can only be achieved by pseudo-static: htaccess This configuration rule implementation

http://www.bkjia.com/PHPjc/874110.html www.bkjia.com true http://www.bkjia.com/PHPjc/874110.html techarticle codeigniter Framework URL Routing summary, codeigniter framework URL URI routing in general, the URI string has a controller class/method that corresponds to it. The various parts of the URI are as follows ...

  • 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.