• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MVC and URLs

#1
[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 Smile

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!

#2
[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 Smile. The URI class is preloaded so no need to load it with $this->load etc Smile.

#3
[eluser]venksster[/eluser]
bastones,

thanks for replying Smile

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?

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

#5
[eluser]bastones[/eluser]
[quote author="venksster" date="1226299540"]bastones,

thanks for replying Smile

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 Smile.

#6
[eluser]venksster[/eluser]
bastones, ray,

that did it! the routing helped map what i needed.

thanks a lot again.

#7
[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 {

    function _remap($request)
    {
        if (is_numeric($request))
        {
            $this->get_user($request);
        }
        else if (method_exists($this, $request))
        {
            $this->$request($this->uri->rsegment(3), $this->uri->rsegment(4));
        }
        else
        {
            show_404();
        }
    }
  
    function get_user()
    {
        // This is now called when you encounter a URL like /users/23
    }
}

#8
[eluser]venksster[/eluser]
Very interesting, Colin. So I suppose we are probably overriding the default _remap(). Nice.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.