CodeIgniter Forums
Routing example in user guide doesn't work? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Routing example in user guide doesn't work? (/thread-2979.html)



Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]CI NC[/eluser]
I've been trying using routes for the first time, I just wanted an alias for a controller, so used the example given in the user guide which claims to do that:

Quote:
Code:
$route['journals'] = "blogs";
Any URL containing the word "journals" in the first segment will be remapped to the "blogs" class.

I understood this would give the routing:
journals/index --> blogs/index
journals/add --> blogs/add

etc.

But -- this doesn't seem to work. Looking at libraries/Router.php, the regex that processes wild cards has a ^ at the beginning and $ at the end, to only match URLS which match exactly.

It looks like there should be another case without the $ at the end.

At the moment, the only way I could make it work was by using backreferences in the routing array:

Code:
$route['journals/(.*)'] = "blogs/$1";

Can anyone else confirm this, or have I misunderstood the user guide?


Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]Matthew Pennell[/eluser]
Yes, your interpretation is correct - you can only remap calls to methods in the journals class by using regex backreferences in the destination string.

I find it most useful for re-directing requests without the name of the controller in:
Code:
$route['(\d+\/[a-z\-]+)'] = "pages/view/$1";
which would redirect /123/my-page-title to pages/view/123.


Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]CI NC[/eluser]
So yes, the user guide is incorrect?

Quote:Any URL containing the word “journals” in the first segment will be remapped to the “blogs” class.
should be
Quote:Any URL containing only the word “journals” in the first segment will be remapped to the “blogs” class.



Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]xenon-dev[/eluser]
Code:
$route['journals'] = "blog";

will only redirect journals/ to blog/. That's all. End of story. If you want ALL requests to journals to be redirected to blog, then

Code:
$route['journals/(*.)'] = "blog/$1";

will do that. And it means that you didn't pay as much attention as needed to the user guide, not misunderstanding Tongue


Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]CI NC[/eluser]
hey, no need to be rude.

Quote:Any URL containing the word “journals” in the first segment will be remapped to the “blogs” class.

The URL /journals/mypage contains the word "journals" in the first segment, but would not be remapped to the "blogs" class. Therefore it is incorrect.


Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]huggiebear[/eluser]
CI NC,

I've read the guide and I concur. The guide is misleading as it doesn't accurately reflect the action of the regex.

Regards
Huggie


Routing example in user guide doesn't work? - El Forum - 09-03-2007

[eluser]xenon-dev[/eluser]
I'm not being rude. However, you've got an issue with that thingie. Let's get it straight:

Code:
journals/somepage != journals/

That means...get the url journals/ (AND ONLY THAT), and redirect it to some url called blogs/. And that's where it stops. It's like the .htaccess file (if you know how it works). You put in:

Code:
RewriteRule ^somedir/$ someotherdir/

and you expect it to automatically do these things:

Code:
RewriteRule ^somedir/first/$ someotherdir/first/
RewriteRule ^somedir/second/$ someotherdir/second/
RewriteRule ^somedir/third/$ someotherdir/third/

# and the list can go on...

It just won't do it. He lives with the idea that he's your master and he won't change his mind Big Grin That's where regex kicks the door and says "no! I'm your master". You know? :)


Routing example in user guide doesn't work? - El Forum - 09-04-2007

[eluser]Matthew Pennell[/eluser]
[quote author="xenon-dev" date="1188860459"]I'm not being rude.[/quote]
Yes, you are. And you were on another thread, too. Try working on your posting style, unless it's your intention to come off like a total p***k. Smile