Changing the default behavior of my application via URI routing |
[eluser]benoa[/eluser]
Good evening, I'm having troubles getting to understand CI routing. My intranet application will have at its heart the group, which is represented by a lowercased string, and everything will happen inside the very group (every controller will need this variable to know which group they are dealing with). As a matter of fact, my urls have to look like the following : http://site.com/awesomegroup/controller/action/ I tried to pass the group name variable through MY_Controller constructor, but from this I didn't find a way to make it work. At first I was thinking of using the session class to set userdata('current_group_id ') but I think my code will turn out to be a mess, and urls won't be as meaningful as they would with routing... I hope someone who had the same kind of concern will read this post and try to help me Kind regards, Benoa
[eluser]Aken[/eluser]
One option - whether it works well for your application I don't know, based on the code - is to set up a generic route that will send you to a URL with the group name excluded (Eg: example.com/groupname/controller/function would go to example.com/controller/function), and then you'd just need to pull the first URI segment and fit it into your code.
[eluser]benoa[/eluser]
Hello Aken, I actually need my urls to have the format http://example.com/groupname/controller/action, but I don't know how to achieve this at all. Do you know how I could do that?
[eluser]Cro_Crx[/eluser]
I'm not 100% sure if this is what you're after, but if i've understood your question, then first you'll need to create a route that redirects url's from this: groupname/controller/action TO controller/action. The route would look like this: Code: $route['groupname/(:any)/(:any)'] = '$1/$2'; Then you can access these segments using the uri helper. For example to get the controller part of the uri use: Code: $this->uri->segment(2); I hope that's what you were after
[eluser]benoa[/eluser]
Can't I keep the groupname in the url, and push controller/action after it? What you suggest seems to work, but I actually would like the group name to be permanently in the url, as if a whole application was built for it. Do you see now? Sorry my first post must have been confusing... My english is not very good...
[eluser]benoa[/eluser]
Hello again, I've read this Phil Sturgeon post, and it was enlightening. Now I get how CI routing works, it's actually fairly simple, just like the rest of the framework. Thank you for trying to help me out
[eluser]benoa[/eluser]
Actually, the solution I come up with is not the most convenient because I have to remap every single controller/action... See my config/routes.php to get what I mean (the application doesn't have many controllers yet): Code: $route['default_controller'] = "home"; I'm posting this because I think maybe someone has a better idea than this... I got to double check before I engage my code in this path... |
Welcome Guest, Not a member yet? Register Sign In |