Welcome Guest, Not a member yet? Register   Sign In
$1, $2... dollar syntax in routes.php
#1

[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";
so that the full routes.php looks like this:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['mycontroller/:num'] = "mycontroller/myfunction/$1";
$route['default_controller'] = "mycontroller";
$route['scaffolding_trigger'] = "";

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 {

    function Mycontroller()
    {
        parent::Controller();
    }
    
    function myfunction($id)
    {
        echo $id
    }
}

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;
with
Code:
echo $this->uri->rsegment(3);
as suggested by http://pr0digy.com/codeigniter/codeignit...i-routing/ but it still outputs $1 as opposed to the actual number entered.
The only way I could get it to work is with
Code:
echo $this->uri->segment(2);
, but this seems like a bad idea.

Please can someone explain how to use the rerouting feature properly with the dollar sign syntax
#2

[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";
as opposed to what is in my previous post.

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.
#3

[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!
#4

[eluser]Unknown[/eluser]
The example needs to be updated. 2011, and it still confused us.
#5

[eluser]toopay[/eluser]
[quote author="xwero" date="1224502456"]The routes are nothing more than an obscured preg_replace array. [/quote] :coolhmm:
#6

[eluser]Unknown[/eluser]
thank you doehoe for the fix. Same scenario here.




Theme © iAndrew 2016 - Forum software by © MyBB