MVC and URLs |
[eluser]venksster[/eluser]
HI all, Well, this might be a n00b question, but well, i AM a n00b as far as CI and MVC are concerned ![]() SO Im trying to design this new app im writing and I was writing down the URLS I would like the users to use: If I want the users to access their profile pages by: http://domain.com/users/$uid, How do I do that, since 'users' would be the controller and $uid will be the function and no args. Currently I would need to revise that to something like http://domain.com/explore/users/$uid Is there a way to get around this and be able to use the first URL? Thanks in advance!
[eluser]bastones[/eluser]
Remember segment-URLs in CodeIgniter go in this order: * Class/Controller * Function * ID/Value You could do it as domain.com/mysite/members/U_ID and all members profiles for instance would be the members() function. You can get values of segments by using $this->uri->segment(n) from the URI Class, such as: $this->uri->segment(3) would fetch the U_ID segment, etc ![]() ![]()
[eluser]venksster[/eluser]
bastones, thanks for replying ![]() in other words, the controller is 'mysite'. That is exactly what I would like to avoid. Instead of http://example.com/mysite/members/U_ID, i would like to be able to use, http://example.com/members/U_ID, where U_ID = $this->uri->segment(2), but segment(2) is always the function. So this cannot be done?
[eluser]ray73864[/eluser]
you could set up a .htaccess rule so that 'http://example.com/member/uid' rewrites to 'http://example.com/mysite/index/uid' or set up a route in CI (dunno how to set that one up)
[eluser]bastones[/eluser]
[quote author="venksster" date="1226299540"]bastones, thanks for replying ![]() in other words, the controller is 'mysite'. That is exactly what I would like to avoid. Instead of http://example.com/mysite/members/U_ID, i would like to be able to use, http://example.com/members/U_ID, where U_ID = $this->uri->segment(2), but segment(2) is always the function. So this cannot be done?[/quote] By default it is as /class/function/value, but if you wish to change this you'll have to see this section of the user guide. But I'm unsure if you can change the first segment as that is associated with the class but check the link incase ![]()
[eluser]venksster[/eluser]
bastones, ray, that did it! the routing helped map what i needed. thanks a lot again.
[eluser]Colin Williams[/eluser]
Routing is good but can be less flexible than employing a _remap() method in the controller. Consider this (which I do often): Code: class Users extends Controller {
[eluser]venksster[/eluser]
Very interesting, Colin. So I suppose we are probably overriding the default _remap(). Nice. |
Welcome Guest, Not a member yet? Register Sign In |