Question

im having problem understanding Slim PHP, by the way i'm new on this framework. This first line of code works.

$app->post('/book',  function() use ($app){

   $app->response();

  /****  Some code here  ***/
});

but i want so separate the function so I try so make it like this

$app->post('/book', 'addBook');

function addBook() {

  $app->response();

  /*** Some code here  ***/
}

but the code above doesn't work. What i'm missing guys?

Was it helpful?

Solution

You can try the getInstance():

$app->post('/book', 'addBook');

function addBook() {

    $app = Slim::getInstance();

    $app->response();

}

OTHER TIPS

You're missing a semicolon after

  $app->response();
                  ^

You also won't be able to reference $app in that function. That notation is only available for anonymous functions. You could do something like this:

$app->get('/book/', function () use ($app) { addBook($app); } );

function addBook($app) {

  var_dump($app->response());

  /*** Some code here  ***/
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top