phroute
phroute copied to clipboard
Extends Controller Index optional parameter NOT ALLOWED
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
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
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 !