mrjgreen / phroute

A super fast PHP router, with route parameters, restful controllers, filters and reverse routing.
Other
712 stars 97 forks source link

Extends Controller Index optional parameter NOT ALLOWED #78

Open fredodiable opened 6 years ago

fredodiable commented 6 years ago

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

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)

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)

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

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

ColonelBlimp commented 6 years ago

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 commented 6 years ago

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 !