$1, $2... dollar syntax in routes.php |
[eluser]doehoe[/eluser]
Please redirect me to a solution to my question if it already exists. In trying to understand how routing works from the tutorial at http://ellislab.com/codeigniter/user-gui...uting.html. As far as I understand it explains that if I have a route specified as Code: $route['mycontroller/:num'] = "mycontroller/myfunction/$1"; Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); and then went to http://domain.com/mycontroller/12 it would reroute to http://domain.com/mycontroller/myfunction/12. Now assuming that the controller looks like this Code: class Mycontroller extends Controller { I would expect the output, based on the entered URI, to be: 12 but instead I get: $1 Surely the $1 is supposed to be converted into whatever number is entered. I tried replacing the Code: echo $id; Code: echo $this->uri->rsegment(3); The only way I could get it to work is with Code: echo $this->uri->segment(2); Please can someone explain how to use the rerouting feature properly with the dollar sign syntax
[eluser]doehoe[/eluser]
After digging around in the system\libraries\Router.php file I discovered the answer which is not clearly identified (but should be) in the CI user guide. I was missing brackets from the route in the routes.php file. So the code should read: Code: $route['mycontroller/(:num)'] = "mycontroller/myfunction/$1"; This is not clearly identified in the URI Routing section of the CodeIgniter User Guide, especially since the first example in this section in the user guide is given without brackets as: Code: $route['product/:num'] = "catalog/product_lookup"; How did I find the answer? Line 273 of system\libraries\Router.php (for CI version 1.6.3) has a strange condition that specifies that dollar variables are only parsed if an open bracket is found in route. Weird. Please correct me if I am being dumb and actually missed something in the user guide.
[eluser]xwero[/eluser]
You are right the user guide should be more clear on this. The routes are nothing more than an obscured preg_replace array. In this light it's not so strange that the values you want have to be surrounded by rounded brackets. But it's good you found it on your own, good effort. Welcome to the community!
[eluser]Unknown[/eluser]
The example needs to be updated. 2011, and it still confused us.
[eluser]toopay[/eluser]
[quote author="xwero" date="1224502456"]The routes are nothing more than an obscured preg_replace array. [/quote] :coolhmm:
|
Welcome Guest, Not a member yet? Register Sign In |