Welcome Guest, Not a member yet? Register   Sign In
Routing bug (is it a bug?)
#1

[eluser]Référencement Google[/eluser]
I think this is a bug but that needs to be discussed.
While using URI Segments to my functions, it's not working with custom routing. For you understand me, here is some tests:

Without custom routing:
Code:
class Page extend Controller
{
    function Page()
    {
        parent::Controller;
    }
    
    function show($url = FALSE)
    {
        echo $url;
    }

}

With a url like http://mysite.com/page/show/test it output "test" so that is correct.

Now defining a custom route like that:
Code:
$route['page/:any'] = "page/show";

And calling the url like this: http://mysite.com/page/test
Nothing is outputed.

To make it work with routing, I have to make the controller like this:
Code:
class Page extend Controller
{
    function Page()
    {
        parent::Controller;
    }
    
    function show()
    {
        // Only this work with routing
        echo $this->uri->segment(2);
    }

}

In user guide I can read "Important: If you are using the URI Routing feature, the segments passed to your function will be the re-routed ones."

So I think this is a bug. Can you confirm it?
#2

[eluser]richthegeek[/eluser]
the URI segments are NOT passed to your function as arguments - you have to use the URI segment functions to retrieve these, as listed in the second block of code.

I don't know if this is a bug or not - it certainly seems like something that could only happen by planning, and I can't see a guaranteeable way of passing the segments as arguments.
#3

[eluser]richthegeek[/eluser]
I seem to remember that you can use regex routing (rather than keyword routing) to allow the sections to be passed as arguments.
#4

[eluser]wiredesignz[/eluser]
In fact semgents ARE passed to functions by CodeIgniter by default, the uri->segment() method is only required if you use _remap() in your controllers.

The OP has incorrect routes set up.

Code:
$route['page/:any'] = "page/show/$1";  // pass :any to page/show method.
#5

[eluser]Référencement Google[/eluser]
The user guide example is:
Code:
$route['product/:any'] = "catalog/product_lookup";
That lead me to the mistake, by the way Wiredesignz your example don't work too and I am pretty sure that is a bug. Can somebody make more tests about it?
#6

[eluser]drfloob[/eluser]
The User Manual's URI Routing - Regular Expressions page has a decent backreference example, so you should start by rereading that section. But to really understand what's going wrong, read up on PHP's Regex Reference - Back References docs and preg_replace( ) function description.

In short, to use backreferences in your routes, you need to include a capture group.

Code:
//  notice the parenthesis around :any.  That's a capture group.
//  $1 ties whatever's inside the first () to the first parameter sent to page's show function

$route['page/(:any)'] = "page/show/$1";
#7

[eluser]Référencement Google[/eluser]
You are absolutely right and, thanks to you Drfloob, that worked.
It would be nice that the "Dereks" take a quick look at the user guide examples in the routing section because things are not very clear about this part and as you see that leads me to a problem.
#8

[eluser]ptrippett[/eluser]
I believe the User guide needs updating with this information as the general examples do not show this functionality
#9

[eluser]drfloob[/eluser]
I agree, the User Guide could use a few more simple regex/back-reference examples. The only example there now is a little complicated, and not very tantalizing to look at unless you already know some regex. Another paragraph and an example or two would probably do some good.

But I don't think that the CI User Guide is the right place to host a common PHP Regex tutorial. If it could make a better case for "this is what a PHP back-reference can do in CI", regex newbs can search google for "php back-reference" and find everything they need to get up to speed Smile
#10

[eluser]ptrippett[/eluser]
There are so many forums and pages etc explaining regex's, PHP official site for example on ereg and preg. Putting a regex tutorial in the user guide would go off topic. But maybe here is a case for a code repository of commonly used code for use in CI.




Theme © iAndrew 2016 - Forum software by © MyBB