Skip to content
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

Extends Controller Index optional parameter NOT ALLOWED #78

Open
fredodiable opened this issue Mar 17, 2018 · 2 comments
Open

Extends Controller Index optional parameter NOT ALLOWED #78

fredodiable opened this issue Mar 17, 2018 · 2 comments

Comments

@fredodiable
Copy link

fredodiable commented Mar 17, 2018

Hello everyone,
I'm having an issue using an extends controller with optional parameter for the index

I would like to create these 2 routes for my api

  • /users => get all users
  • /users/1 => get user with id = 1

If I do it without a controller it works
$router->get("/users", function() { return "get all users"; });
$router->get("/users/{id:i}?", function($id = null) { return "get user id = $id"; });

Now with a controller
TEST 1
$router->controller("/users", "UsersController");

With a public function getIndex($pId = null)

  • /users => i got a METHOD NOT ALLOWED error
  • /users/1 => work

TEST 2
Trying with the following route now
$router->controller("/users/{id:i}?", "UsersController");

Method parameter is not $id else we got "Cannot use the same placeholder 'id' twice"

With a public function getIndex($pId = null)

  • /users => i got a METHOD NOT ALLOWED error
  • /users/1 => i got a METHOD NOT ALLOWED error

TEST 3
I need to add this method for both routes to work
public function anyIndex($pId = null)

  • /users => work
  • /users/1 => work

Why the first test don't work?
Did I miss something ?
Is this a bug ?

Thx for answers :)

Edit :
After some others tests
TEST 2 work directly from extends controller but not if defined in base controller

@fredodiable fredodiable changed the title Controller Index optional parameter NOT ALLOWED Extends Controller Index optional parameter NOT ALLOWED Mar 17, 2018
@ColonelBlimp
Copy link

ColonelBlimp commented May 13, 2018

Hi,

I know that your question was some months back, but I wonder if this might help:

namespace Tester {

    abstract class BaseController
    {
        public function indexAction(): string
        {
            return 'Called: ' . __METHOD__;
        }
    }

    class Controller extends BaseController
    {
        public function listingAction(string $page): string
        {
            return 'Called: ' . __METHOD__ . "\n" . 'Page: ' . $page;
        }

        public function productAction(string $query): string
        {
            return 'Called: ' . __METHOD__ . "\n" . 'Query string: ' . $query;
        }
    }
}

namespace {
    include dirname(__DIR__) . '/vendor/autoload.php';

    use Phroute\Phroute\Dispatcher;
    use Phroute\Phroute\RouteCollector;

    $collector = new RouteCollector();

    $collector->get('', ['\\Tester\\Controller', 'indexAction']);
    $collector->get('listing/{page}', ['\\Tester\\Controller', 'listingAction']);
    $collector->get('product?{query}', ['\\Tester\\Controller', 'productAction']);

    $dispatcher =  new Dispatcher($collector->getData());

    echo $dispatcher->dispatch('GET', '/'), "\n";
    echo $dispatcher->dispatch('GET', 'listing/2'), "\n";
    echo $dispatcher->dispatch('GET', 'product?action=edit&id=coffee'), "\n";
}

The output should be:

Called: Tester\BaseController::indexAction
Called: Tester\Controller::listingAction
Page: 2
Called: Tester\Controller::productAction
Query string: action=edit&id=coffee

@fredodiable
Copy link
Author

Hi,
this worked fine, look like this now

$router->post("/users", [UsersController::class, "postIndex"]);
$router->get("/users", [UsersController::class, "getIndex"]);
$router->get("/users/{id:i}?", [UsersController::class, "getIndex"]);
$router->put("/users", [UsersController::class, "putIndex"]);
$router->delete("/users", [UsersController::class, "deleteIndex"]);
$router->delete("/users/{id:i}?", [UsersController::class, "deleteIndex"]);

I will create an auto register method to avoid multi line spamming for each controller
I was able to remove the anyIndex method from my controller
And now the route regex work too !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants