Allow :alpha in routes for CodeIgniter 2.0.1 Reactor.


/ Published in: PHP
Save to your folder(s)

Just save this as MY_Router.php and upload to application/core/ and now you'll be able to use us :alpha along with :num and :any in your routes to signify that you just want to match a-z.


Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 5.1.6 or newer
  6.  *
  7.  * @package CodeIgniter
  8.  * @author ExpressionEngine Dev Team
  9.  * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10.  * @license http://codeigniter.com/user_guide/license.html
  11.  * @link http://codeigniter.com
  12.  * @since Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * Router Class
  20.  *
  21.  * Parses URIs and determines routing
  22.  *
  23.  * @package CodeIgniter
  24.  * @subpackage Libraries
  25.  * @author ExpressionEngine Dev Team
  26.  * @category Libraries
  27.  * @link http://codeigniter.com/user_guide/general/routing.html
  28.  */
  29. class MY_Router extends CI_Router {
  30.  
  31. function __construct()
  32. {
  33. parent::__construct();
  34. }
  35.  
  36. /**
  37. * Parse Routes
  38. *
  39. * This function matches any routes that may exist in
  40. * the config/routes.php file against the URI to
  41. * determine if the class/method need to be remapped.
  42. *
  43. * @access private
  44. * @return void
  45. */
  46. function _parse_routes()
  47. {
  48. // Turn the segment array into a URI string
  49. $uri = implode('/', $this->uri->segments);
  50.  
  51. // Is there a literal match? If so we're done
  52. if (isset($this->routes[$uri]))
  53. {
  54. return $this->_set_request(explode('/', $this->routes[$uri]));
  55. }
  56.  
  57. // Loop through the route array looking for wild-cards
  58. foreach ($this->routes as $key => $val)
  59. {
  60. /*
  61. // OLD CODEIGNITER WILD-CARD REGEX
  62.  
  63. // Convert wild-cards to RegEx
  64. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  65.  
  66. */
  67.  
  68. // Convert wild-cards to RegEx
  69. // Additionally added :alpha to the wild cards.
  70. // @Author Josh Manders
  71. // @Website joshmanders.com
  72. $wild_cards = array(':any' => '.+', ':num' => '[0-9]+', ':alpha' => '[a-zA-Z]+');
  73. foreach ($wild_cards as $wild_card => $regex)
  74. {
  75. $key = str_replace($wild_card, $regex, $key);
  76. }
  77.  
  78. // Does the RegEx match?
  79. if (preg_match('#^'.$key.'$#', $uri))
  80. {
  81. // Do we have a back-reference?
  82. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  83. {
  84. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  85. }
  86.  
  87. return $this->_set_request(explode('/', $val));
  88. }
  89. }
  90.  
  91. // If we got this far it means we didn't encounter a
  92. // matching route so we'll set the site default route
  93. $this->_set_request($this->uri->segments);
  94. }
  95. }
  96. // END Router Class
  97.  
  98. /* End of file MY_Router.php */
  99. /* Location: ./app/core/MY_Router.php */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.