CodeIgniter Forums
Routing: Passing entire expression as unique argument - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Routing: Passing entire expression as unique argument (/thread-29321.html)



Routing: Passing entire expression as unique argument - El Forum - 04-06-2010

[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


Routing: Passing entire expression as unique argument - El Forum - 04-06-2010

[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);
$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.

Cheers
Tim


Routing: Passing entire expression as unique argument - El Forum - 04-07-2010

[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”;
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:
Code:
// 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:
Code:
// 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.


Routing: Passing entire expression as unique argument - El Forum - 04-07-2010

[eluser]n0xie[/eluser]
Don't need routes or any voodoo. Just use this:
Code:
function method()
    {
        $slugs = func_get_args();
        echo implode('/',$slugs);
    }



Routing: Passing entire expression as unique argument - El Forum - 04-07-2010

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


Routing: Passing entire expression as unique argument - El Forum - 04-07-2010

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