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:
example.com/class/function/id/
In some instances, however, you may want to remap this relationship so that a different class/function can be called instead of the one corresponding to the URL.
For example, lets say you want your URLs to have this prototype:
example.com/product/1/
example.com/product/2/
example.com/product/3/
example.com/product/4/
Normally the second segment of the URL is reserved for the function name, but in the example above it instead has a product ID. To overcome this, CodeIgniter allows you to remap the URI handler.
Setting your own routing rules
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
Wildcards
A typical wildcard route might look something like this:
$route['product/:num'] = "catalog/product_lookup";
In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. In the above example, if the literal word "product" is found in the first segment of the URL, and a number is found in the second segment, the "catalog" class and the "product_lookup" method are instead used.
You can match literal values or you can use two wildcard types:
:num
:any
:num will match a segment containing only numbers.
:any will match a segment containing any character.
Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
Examples
Here are a few routing examples:
$route['journals'] = "blogs";
A URL containing the word "journals" in the first segment will be remapped to the "blogs" class.
$route['blog/joe'] = "blogs/users/34";
A URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".
$route['product/:any'] = "catalog/product_lookup";
A URL with "product" as the first segment, and anything in the second will be remapped to the "catalog" class and the "product_lookup" method.
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
A URL with "product" as the first segment, and anything in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.
Important: Do not use leading/trailing slashes.
Regular Expressions
If you prefer you can use regular expressions to define your routing rules. Any valid regular expression is allowed, as are back-references.
Note: If you use back-references you must use the dollar syntax rather than the double backslash syntax.
A typical RegEx route might look something like this:
$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";
In the above example, a URI similar to products/shirts/123 would instead call the shirts controller class and the id_123 function.
You can also mix and match wildcards with regular expressions.
Reserved Routes
There are two reserved routes:
$route['default_controller'] = 'welcome';
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. In the above example, the "welcome" class would be loaded. You are encouraged to always have a default route otherwise a 404 page will appear by default.
$route['scaffolding_trigger'] = 'scaffolding';
This route lets you set a secret word, which when present in the URL, triggers the scaffolding feature. Please read the Scaffolding page for details.
Important: The reserved routes must come before any wildcard or regular expression routes.
Source Scaffolding
| Hàm str_replace trong PHP < Lùi | Tiếp theo > Đăng nhập vào Yahoo sử dụng giao thức YMSG |
|---|
- 21/08/2009 20:37 - Một số hàm thao tác với files trong PHP
- 15/08/2009 21:11 - Website Monitoring PHP
- 24/07/2009 18:28 - ooeLite Web application development framework
- 08/07/2009 08:58 - Hàm XSS Clean chống Cross Site Scripting Hacks
- 06/07/2009 10:48 - Hàm str_replace trong PHP
- 05/07/2009 16:13 - Đăng nhập vào Yahoo sử dụng giao thức YMSG
- 28/06/2009 21:15 - Viết chương trình download manager đơn giản
- 10/06/2009 16:00 - Lập trình PHP - Đếm số người trực tuyến
- 09/06/2009 23:50 - Chuỗi (xâu) và các hàm xử lý chuỗi trong PHP
- 09/06/2009 16:53 - Hàm PHP: substr_compare
Download Game bóng đá PES8 Việt Nam
Tải game bóng đá FiFa 09
Tải game Dragon ball Z - 7 viên ngọc rồng
Tải Game Bóng Đá Pro Evolution Soccer 6 Việt Nam
Download PES 2011 patch Việt Nam PESVN
Tải Game Bóng Đá FIFA 2009 RIP 1 CD
Download Pro Evolution Soccer 2011
Download Game THE SIMS 3
Tải Game bóng đá PES 2009
Download PES 2011 demo
Thay đổi màn hình đăng nhập Windows 7 bằng tay
Một số hàm hay dùng trong JavaScript
Kiểm soát hoàn toàn trỏ chuột bằng bàn phím với NeatMouse
Choáng với concept của Windows Phone 8
Những đặc điểm ưu việt của Windows Phone
Ứng dụng gọi điện VoIP Viber sắp có mặt trên Windows Phone
Game Diablo 3 sắp có mặt trên Windows Phone Marketplace
37 trang css & xhtml web template miễn phí tuyệt đẹp
Những điều cơ bản cần biết về bàn phím chơi game - Gaming Keyboard
L.A. Noire: The Complete Edition - THETA [Full ISO/Action/2011] [13.37GB]
Download 7554 demo
Download Game PC, cập nhật liên tục (torrent)
PES 2012 ra mắt bản mở rộng đầu tiên vào 11/10
PES2012 - Full version - Ngôn ngữ và bình luận tiếng Anh
PES2012 - Blurry disable làm mịn đồ họa
PES 2011 PATCH 4.1 - TRANSFER UPDATE - RELEASED
Bộ sưu tập free PrestaShop Themes phần 3
Bộ sưu tập theme Prestashop miễn phí phần 2
Top 10 Free PrestaShop Themes
Download Prestashop 1.5
Download PrestaShop 1.4
Bổ sung Memcached vào PrestaShop 1.4
Hướng dẫn tích hợp Ngân Lượng vào PrestaShop
Sửa lỗi invalid carrier or no carrier selected trong PrestaShop
Cài đặt PrestaShop
Thiết kế giao diện PrestaShop Theme
Hướng dẫn cài đặt themes trong PrestaShop
Hướng dẫn cách thêm ngôn ngữ cho PrestaShop
Bộ sưu tập Giao diện Premium PrestaShop Themes chuyên nghiệp
Giới thiệu về website thương mại điện tử 2.0 Prestashop
10 smartphone “bom xịt” năm 2010
Pro Evolution Soccer 2010 - Sự trở lại của vị vua [RELOADED][PC Game]
Bài 1: Làm quen với hệ quản trị cơ sở dữ liệu Microsoft Access


