Routing: Passing entire expression as unique argument

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:

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";

$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]

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

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.

$year = $this->uri->segment(3);
$month = $this->uri->segment(4);
$day = $this->uri->segment(5);

(I might have the indexes wrong )

And then do what you need to from there.

Hope that helps.


The routing feature is really only a string handling functon. The examples you give, like
$route[‘controller/method/(.*)$’] = “controller/method/$1”;
are not changing the string at all! After this route, the url is exactly the same!

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:
// imagine the requested url is /controller/method/path/to/file
$path = array_slice( $this->uri->segment_array(), 2 );
// now $path will be an array containing path, to, file
$path = join('/', $path);
// now $path will be 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:
// imagine the requested url is encoded: /controller/method/path % 2f to % 2f file
$path = $this->uri->segment(3);
// now $path will be path % 2f to % 2f file
$path = urldecode($path);
// now $path will be path/to/file
Note: the spaces in the url in this example are there because otherwise the forum translates the url encoded string.

Don't need routes or any voodoo. Just use this:
function method()
        $slugs = func_get_args();
        echo implode('/',$slugs);

You are right n0xie. That is much cleaner. I had not realised that the remaining arguments are passed to the Controller's methods.

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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