Routing: Passing entire expression as unique argument |
[eluser]Unknown[/eluser]
Hello, I'm trying various configurations with the Code Igniter routing feature. I'm developing a filebrowser-like webapp, and my goal is: Given this URL: http://example.com/app/controller/method/path/to/file I want that CodeIgniter call my method on controller passing it the rest of the url (something like $controller->method("path/to/file") I tried with: $route['controller/method/(:any)'] = "controller/method/$1"; and $route['controller/method/(.*)$'] = "controller/method/$1"; But I only get the first path component as argument to my method. (I tried various regexes and nothing) How can I get this behaviour? Thank you
[eluser]Tim Brownlaw[/eluser]
Hi, You need to use the URI Class to obtain the info from the segments in the URL. Check out the userguide on that. In your case you are not altering the URL at all so you do not need to use routing. Even if you are using routing you still need to use the URI Class. When you would use routing Routing is used if you want to have your URL like.. /articles/2010/June/24 Which would need to be routed to /articles/display/2010/June/24 controller = articles and the method = display with the rest of the information tacked on. In routes.php $route['articles/:any'] = "articles/display/$1"; creating the link above... SO you are translating your url of /articles/2010/June/24 to /articles/display/2010/June/24 and you then need to read in the segments with the information you are after, that being 2010/June/24. In your Articles Controller, in the display method you would have this snippet. Code: $year = $this->uri->segment(3); (I might have the indexes wrong ) And then do what you need to from there. Hope that helps. Cheers Tim
[eluser]mddd[/eluser]
The routing feature is really only a string handling functon. The examples you give, like Code: $route[‘controller/method/(.*)$’] = “controller/method/$1”; The problem is that you are trying to pass a string containing slashes inside the url. That doesn't work because CI chops it up. I see two options: one is to get the entire segment array and chop off the first part (controller, method). Like so: Code: // imagine the requested url is /controller/method/path/to/file The other option is to encode the path to that it doesn't contain slashes anymore. Then CI will see it as one part. Like so: Code: // imagine the requested url is encoded: /controller/method/path % 2f to % 2f file
[eluser]n0xie[/eluser]
Don't need routes or any voodoo. Just use this: Code: function method()
[eluser]mddd[/eluser]
You are right n0xie. That is much cleaner. I had not realised that the remaining arguments are passed to the Controller's methods.
[eluser]Unknown[/eluser]
n0xie has the best correct answer. I thought with a workarround like this, but I'm a newbie in PHP and I didn't know about the correct function to get all the args. Thanks you very much to all of you!. |
Welcome Guest, Not a member yet? Register Sign In |