New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters not working as expected? #65

Closed
jamesaspence opened this Issue Sep 26, 2014 · 6 comments

Comments

Projects
None yet
3 participants
@jamesaspence

jamesaspence commented Sep 26, 2014

Hey guys, I have optional parameters set in a URL as follows:

$routes = [
    [
        "name" => "API",
        "path" => "/api/{version}/{class}{/id,function,third}/"
    ]
];

$newRoute = $this->router->add($route["name"], $route["path"]);
    if(isset($route["tokens"]))
        $newRoute->addTokens($route["tokens"]);
    if(isset($route["values"]))
        $newRoute->addValues($route["values"]);

and I have the controller matching it printing it out like this:

if(isset($this->route->params["id"]))
    echo "id = " . $this->route->params["id"] . "<br><br>";
if(isset($this->route->params["function"]))
    echo "function = " . $this->route->params["function"] . "<br><br>";
if(isset($this->route->params["third"]));
    echo "third = " . $this->route->params["third"] . "<br><br>";
exit;

So that if you have a URL like /api/1/class/id/function/third/, it'll print out like:

id = id
function = function
third = third

however, if I only specify a single parameter (i.e. /api/1/class/id/), it will still print like this:

id = id
third = 

So it always sets third, regardless of whether or not it's been specified.
Anybody know why? Is it my syntax that's causing the issue?

@harikt

This comment has been minimized.

Show comment
Hide comment
@harikt

harikt Sep 27, 2014

Member

This is where you are doing wrong .

if(isset($this->route->params["third"]));

See the ; . Consider coding with PSR-1, PSR-2 so your if always have { and ending } .

Thank you.

Member

harikt commented Sep 27, 2014

This is where you are doing wrong .

if(isset($this->route->params["third"]));

See the ; . Consider coding with PSR-1, PSR-2 so your if always have { and ending } .

Thank you.

@harikt

This comment has been minimized.

Show comment
Hide comment
@harikt

harikt Sep 27, 2014

Member

The best way to look is var_dump($route->params); . Some may argue not to use var_dump though ;) .

Member

harikt commented Sep 27, 2014

The best way to look is var_dump($route->params); . Some may argue not to use var_dump though ;) .

@harikt harikt closed this Sep 27, 2014

@jamesaspence

This comment has been minimized.

Show comment
Hide comment
@jamesaspence

jamesaspence Sep 29, 2014

Welp.. I feel dumb.
Thanks for the help!

jamesaspence commented Sep 29, 2014

Welp.. I feel dumb.
Thanks for the help!

@harikt

This comment has been minimized.

Show comment
Hide comment
@harikt

harikt Sep 29, 2014

Member

You are welcome 👍 .

Member

harikt commented Sep 29, 2014

You are welcome 👍 .

@pmjones

This comment has been minimized.

Show comment
Hide comment
@pmjones

pmjones Sep 29, 2014

Member

Wow, @harikt , that's a great great catch. I would not have seen that.

@ThaPastafarian As noted by @harikt PSR-2 will save you here. Specifically, always always always use braces on if/else bodies. The habit of doing so will save you much time and frustration.

Member

pmjones commented Sep 29, 2014

Wow, @harikt , that's a great great catch. I would not have seen that.

@ThaPastafarian As noted by @harikt PSR-2 will save you here. Specifically, always always always use braces on if/else bodies. The habit of doing so will save you much time and frustration.

@harikt

This comment has been minimized.

Show comment
Hide comment
@harikt

harikt Sep 30, 2014

Member

I did tried myself ;) . Just looking at code have not helped me also :-) . Yes PSR-2 will help a lot in these cases.

Member

harikt commented Sep 30, 2014

I did tried myself ;) . Just looking at code have not helped me also :-) . Yes PSR-2 will help a lot in these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment