Welcome Guest, Not a member yet? Register   Sign In
My routes aren't passing arguments
#1

[eluser]NSGReaper[/eluser]
So I'm currently working on the file download script for a site. Files can be downloaded either by using: http://mysite/download/34 or http://mysite/download/project.pdf. That is the plan at least, these should be routed to files/getbyid and files/getbyname respectively but while I know that the right function is called no arguments are being passed to it.

Code:
$route['download/:num'] = "files/getbyid";
$route['download/:any'] = "files/getbyname";

Code:
class Files extends Controller {
    function getbyid($id = -1)
    {
        if ($id = -1) {
            die("No file id specified");
        } else { echo $id; }
    }
    
    function getbyname($file = "")
    {
        if ($file = "") {
            die("No file name specified");
        } else { echo $file; }
    }
}

With it setup like this I always get the die() message. I have been able to make it work using regex like so:

Code:
$route['download/([0-9]*)'] = "files/getbyid/$1";
$route['download/(.*)'] = "files/getbyname/$1";


However it was my understanding that these were synonymous. Have I misunderstood the user guide or is something wrong?
#2

[eluser]MikePearce[/eluser]
You have to use the regex if you want to pass things to the controllers. Just using the :num and :any is just a means of mapping one URL to another when you don't know what the first URL might be.
#3

[eluser]NSGReaper[/eluser]
Thanks, I guess I made an assumption rather than misunderstood. Anyway, using regex it works beautifully.
#4

[eluser]alpar[/eluser]
Code:
$route['download/(:num)'] = "files/getbyid/$1";
$route['download/(:any)'] = "files/getbyname/$1";

also works
#5

[eluser]NSGReaper[/eluser]
Ah, thanks alpar. I was hoping to find an elegant solution. I think I understand it now too.




Theme © iAndrew 2016 - Forum software by © MyBB