Respect/Rest

When not passing parameters

Closed this issue · 8 comments

I'm trying to do something link this:

$r3 = new Respect\Rest\Router('/labs/respect');

$r3->any('/**', function() {
    return '404 error';
});

$r3->any('/*/*', function($r1 = null, $r2 = null) {
    return 'two routes setted';
})->when(function($r1, $r2) {
    return ( !is_null( $r1 ) && !is_null( $r2 ) );
});

But, in the when callback function, the $r1, and $r2 always come NULL.

That's a bug or i'm doing something wrong?

Seems like a bug. Are ther any other routes in your router? Which URL are you calling to test that request?

Thanks for the feedback!

In this test, I only have this two routes. But in another project, I have like 40 routes, and get the same error.

In this test, the URLs i'm calling are something like this:

I always get the 404 error response.

If I var_dump the $r1 and $r2, they always come NULL, in all URLs above.

I tried and I can't reproduce this. I've cloned the latest build (develop branch) and set up an index.php file with these contents:

<?php
spl_autoload_register(include 'Respect/Loader.php');
use Respect\Rest\Router;

$r3 = new Router;

$r3->any('/*/*', function ($r1 = null, $r2 = null) {
    return print_r($r1.$r2, true);
})->when(function ($r1, $r2) {
    var_dump($r1, $r2);
    return !is_null($r1) && !is_null($r2);
});

Respect\Loader is installed via PEAR, any other PSR-0 autoloader should work though.

Then I ran that script using the PHP bundled server with this command: php -S localhost:8666 and accessed http://localhost:8666/a1/a2, the output shows string(1) "a" string(1) "b" ab as expected. For the URLs http://localhost:8666/a1, http://localhost:8666/a2 and http://localhost:8666/ the output is 404, because there are parameters missing.

This is my full output for php -v && php -S localhost:8666:

alganet@alganet-ultrabook:~/Projects/Respect/Rest/tests/issues/78$ php -v && php -S localhost:8666
PHP 5.4.8-1~dotdeb.0 (cli) (built: Oct 18 2012 21:19:48) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
PHP 5.4.8-1~dotdeb.0 Development Server started at Mon Nov 12 00:09:26 2012
Listening on http://localhost:8666
Document root is /home/alganet/Projects/Respect/Rest/tests/issues/78

The /*/* is a pretty generic route. More specific routes can take precedence over generic ones (the more parameters you have, more generic is the route).

Let me know if that data and test helps you out. If don't, we probably have a bug targeting a specific PHP or Respect version. Which versions are you using?

Thanks again for reporting and keeping the feedback flowing =D

Weird, i'm install via composer, the version 0.5.1 and user the composer autoload.

{
    "require" : {
        "Respect/Rest" : "0.5.1"
    }
}

I'm gonna try to clone the develop branch and run this test, then come back here and give the feedback.

Is this route not taking all the action?

$r3->any('/**', function() {
    return '404 error';
});

If I remember correctly it works on FIFO but I could be mistaken. Try and add the catch all to the bottom ie. the last route definition in the list.

@luisdalmolin did you find resolve? Can we close this issue?

Thank you for bringing this to our attention.

Yeah, the route don't work with the catch all parameters for 404 error.

$r3->any('/**', function() {
    return '404 error';
});

If I remove this route, works fine.
Thank's!

Awesome!

thumbs up panda