Skip to content

Instantly share code, notes, and snippets.

@narfbg
Created October 26, 2012 09:48
Show Gist options
  • Save narfbg/3957904 to your computer and use it in GitHub Desktop.
Save narfbg/3957904 to your computer and use it in GitHub Desktop.
Fix for CodeIgniter issue #658
diff --git a/system/core/Router.php b/system/core/Router.php
index 5bc0530..db73cb2 100644
--- a/system/core/Router.php
+++ b/system/core/Router.php
@@ -368,7 +368,7 @@ class CI_Router {
foreach ($this->routes as $key => $val)
{
// Convert wild-cards to RegEx
- $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
+ $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 8308cd6..b63d51e 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -384,6 +384,7 @@ Bug fixes for 3.0
- Fixed a bug (#1811) - :doc:`URI Library <libraries/uri>` didn't properly cache segments for ``uri_to_assoc()`` and ``ruri_to_assoc()``.
- Fixed a bug (#1506) - :doc:`Form Helpers <helpers/form_helper>` set empty *name* attributes.
- Fixed a bug (#59) - :doc:`Query Builder <database/query_builder>` method ``count_all_results()`` ignored the DISTINCT clause.
+- Fixed a bug (#658) - :doc:`Routing <general/routing>` wildcard **:any** didn't work as advertised and matched multiple URI segments instead of all characters within a single segment.
Version 2.1.3
=============
diff --git a/user_guide_src/source/general/routing.rst b/user_guide_src/source/general/routing.rst
index c039370..52ade3d 100644
--- a/user_guide_src/source/general/routing.rst
+++ b/user_guide_src/source/general/routing.rst
@@ -29,7 +29,7 @@ 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
+Regular Expressions.
Wildcards
=========
@@ -47,7 +47,11 @@ segment of the URL, and a number is found in the second segment, the
You can match literal values or you can use two wildcard types:
**(:num)** will match a segment containing only numbers.
-**(:any)** will match a segment containing any character.
+**(:any)** will match a segment containing any character (except for '/', which is the segment delimiter).
+
+.. note:: Wildcards are actually aliases for regular expressions, with
+ **:any** being translated to **[^/]+** and **:num** to **[0-9]+**,
+ respectively.
.. note:: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.
@@ -104,10 +108,22 @@ rules. Any valid regular expression is allowed, as are back-references.
A typical RegEx route might look something like this::
- $route['products/([a-z]+)/(\d+)'] = "$1/id_$2";
+ $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.
+call the shirts controller class and the id_123 method.
+
+With regular expressions, you can also catch a segment containing a
+forward slash ('/'), which would usually represent the delimiter between
+multiple segments.
+For example, if a user accesses a password protected area of your web
+application and you wish to be able to redirect them back to the same
+page after they log in, you may find this example useful::
+
+ $route['login/(.+)'] = 'auth/login/$1';
+
+That will call the auth controller class and its ``login()`` method,
+passing everything contained in the URI after *login/* as a parameter.
You can also mix and match wildcards with regular expressions.
diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst
index 31a5c07..41425e4 100644
--- a/user_guide_src/source/installation/upgrade_300.rst
+++ b/user_guide_src/source/installation/upgrade_300.rst
@@ -52,11 +52,12 @@ Step 5: Update your config/database.php
***************************************
Due to 3.0.0's renaming of Active Record to Query Builder, inside your `config/database.php`, you will
-need to rename the `$active_record` variable to `$query_builder`.
+need to rename the `$active_record` variable to `$query_builder`
+::
- $active_group = 'default';
- // $active_record = TRUE;
- $query_builder = TRUE;
+ $active_group = 'default';
+ // $active_record = TRUE;
+ $query_builder = TRUE;
*******************************
Step 6: Move your errors folder
@@ -64,15 +65,35 @@ Step 6: Move your errors folder
In version 3.0.0, the errors folder has been moved from _application/errors* to _application/views/errors*.
+*******************************************************
+Step 7: Update your config/routes.php containing (:any)
+*******************************************************
+
+Historically, CodeIgniter has always provided the **:any** wildcard in routing,
+with the intention of providing a way to match any character **within** an URI segment.
+
+However, the **:any** wildcard is actually just an alias for a regular expression
+and used to be executed in that manner as **.+**. This is considered a bug, as it
+also matches the / (forward slash) character, which is the URI segment delimiter
+and that was never the intention. In CodeIgniter 3, the **:any** wildcard will now
+represent **[^/]+**, so that it will not match a forward slash.
+
+There certainly many developers that have utilized this bug as an actual feature.
+If you're one of them and want to match a forward slash, please use the **.+**
+regular expression::
+
+ (.+) // matches ANYTHING
+ (:any) // matches any character, except for '/'
+
****************************************************************************
-Step 7: Check the calls to Array Helper's element() and elements() functions
+Step 8: Check the calls to Array Helper's element() and elements() functions
****************************************************************************
The default return value of these functions, when the required elements
don't exist, has been changed from FALSE to NULL.
***************************************************************
-Step 8: Remove usage of (previously) deprecated functionalities
+Step 9: Remove usage of (previously) deprecated functionalities
***************************************************************
In addition to the ``$autoload['core']`` configuration setting, there's a number of other functionalities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment