mrjgreen / phroute

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

Weird behaviour when using controllers #69

Open ghost opened 7 years ago

ghost commented 7 years ago

Something that caught me off guard suddenly after many hours of programming:

Routes.php: $router->controller('/term/config/forms', 'TERM\\FormConfigController');

The specific controller.php file: `namespace TERM;

class FormConfigController extends \Controller { public static function getIndex(){ //do something... } public static function getActivate($formID){ return A; } public static function getEditform($formID, $action = null, $actionValue = null){ return B; } `

If I now use the following URL: some.domain/term/config/forms/editform/20 It results in the getActivate method being called. However, and this is quite peculiar, if I swap getActivate and getEditForm methods around, then it works as expected.

Could someone please explain if this is either a bug, a design flaw from my side, or just a strange way of how this system works?

crichardson9 commented 6 years ago

I'd do it this way:

$router->get('/term/config/forms/editform/{id:i}', ['TERM\\FormConfigController, 'getEditForm']);

and:

public static function getEditForm($formId)
{
    return $formId;
}

If you want to continue using the controller method, I believe you must ensure that your function has the same parameters. For example:

public static function getEditform($formID)
{
    return B;
}