php - Slim router url calls controller without hardcoding the map -


i generating slim error don't understand: missing argument 1 {closure}()

what trying automatically map controller pulled out of url controller file, without hardcoding filename. works fine if go somesite.com/accounting, when comment out hardcode route accounting not work, error missing argument 1 {closure}().

$url = str_replace( web_path, '', $_server['request_uri']); $split_url = explode('/', $url); $controller_name = $split_url[1];   $app = new \slim\slim(); $app->get('/', function ()  {    echo 'home'; }); $app->get('/accounting',  function ()  {    $controller = new accounting; }); $app->get('/purchaseorders',  function ()  {    $controller = new purchaseorders; }); //this should work not... $app->get('/'. $controller_name,  function ($controller_name)  {   echo $controller_name;    $controller = new $controller_name; }); $app->get('test.php', function ()  {    $controller = new test; }); $app->run(); 

when using parameter in routes, have write route path follows:

$app->get('/:controller_name',  function ($controller_name)  {     echo $controller_name;     $controller = new $controller_name; }); 

all parameters strings : prepended it. can use multiple parameters separated slashes if need.

using code above declare route, slim internally fill $controller_name argument value in parameter.

when accessing /accounting, argument hold value 'accounting'.

refer routing parameter documentation on more details how use parameters slim routes.


Comments