From d4e95072203a5cf4f1d50d16fe3e490f275a4307 Mon Sep 17 00:00:00 2001
From: admin
Date: Sat, 26 Aug 2006 01:15:06 +0000
Subject:
---
system/application/config/routes.php | 2 ++
system/libraries/Router.php | 19 +++++++++++++++----
user_guide/general/changelog.html | 3 ++-
user_guide/general/routing.html | 29 ++++++++++++++++++++++++++---
4 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/system/application/config/routes.php b/system/application/config/routes.php
index 622bf88e6..5167f7569 100644
--- a/system/application/config/routes.php
+++ b/system/application/config/routes.php
@@ -46,4 +46,6 @@ $route['scaffolding_trigger'] = "scaffolding";
// Define your own routes below -------------------------------------------
+$route['products\/([a-z]+)\/(\d+)'] = "$1/a$2";
+
?>
\ No newline at end of file
diff --git a/system/libraries/Router.php b/system/libraries/Router.php
index abc253eff..b61dfb79c 100644
--- a/system/libraries/Router.php
+++ b/system/libraries/Router.php
@@ -300,16 +300,27 @@ class CI_Router {
}
// Loop through the route array looking for wildcards
- foreach ($this->routes as $key => $val)
+ foreach (array_slice($this->routes, 1) as $key => $val)
{
if (count(explode('/', $key)) != $num)
continue;
-
- if (preg_match("|".str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key))."$|", $uri))
- {
+
+ // Convert wildcards to RegEx
+ $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
+
+ // Does the regex match this URI ?
+ if (preg_match('|^'.$key.'$|', $uri))
+ {
+ // Do we have a replacemnt?
+ if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
+ {
+ $val = preg_replace('|^'.$key.'$|', $val, $uri);
+ }
+
$this->_compile_segments(explode('/', $val), TRUE);
break;
}
+
}
}
// END set_method()
diff --git a/user_guide/general/changelog.html b/user_guide/general/changelog.html
index 0bcb165da..a03d6b19f 100644
--- a/user_guide/general/changelog.html
+++ b/user_guide/general/changelog.html
@@ -69,8 +69,9 @@ Change Log
- Added Hooks feature, enabling you to tap into and modify the inner workings of the framework without hacking the core files.
-- Added the ability to replace core system classes with your own classes.
+- Added the ability to replace core system classes with your own classes.
- Added support for % character in URL.
+- Updated the Routing feature to accept regular expressions within routing rules.
- Moved the MIME type array out of the Upload class and into its own file in the applications/comfig/ folder.
- Removed a strtolower() call that was changing URL segments to lower case.
- Deprecated the hash() function due to a naming conflict with a native PHP function with the same name. Please use dohash() instead.
diff --git a/user_guide/general/routing.html b/user_guide/general/routing.html
index c6e8bd9cf..977698b9f 100644
--- a/user_guide/general/routing.html
+++ b/user_guide/general/routing.html
@@ -85,8 +85,13 @@ To overcome this, Code Igniter 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
-you can use to specify your own routing criteria. A typical route might look something like this:
+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";
@@ -125,8 +130,21 @@ Higher routes will always take precedence over lower ones.
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 then the double backslash syntax.
+
+A typical RegEx route might look something like this:
+
+$route['products\/([a-z]+)\/(\d+)'] = "$1/id_$2";
-Reserved Route
+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:
@@ -144,6 +162,11 @@ Please read the Scaffolding page for details.
+
+
+
+
+
--
cgit v1.2.3-24-g4f1b