mrjgreen/phroute

Weird behaviour when using controllers

Thyone opened this issue · 1 comments

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?

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;
}