0

I'm trying to use flight PHP framework for routing and medoo framework for database usage.

//connect database
$database = new medoo([
'database_type' => 'sqlite',
'database_file' => 'db.sqlite'
]);
//my function
function database($database){
$database->insert("ppl", [
"fn" => "joe","ln"=>"doe"]);

}
//
Flight::route('/add/', array(database($database)));

How to invoke my function with argument from this place:

Flight::route('/add/','database')

Tried different variants but getting errors.

4

1 回答 1

0

我不知道medooflight,但您也许可以使用匿名函数use

Flight::route('/add/',
              function() use($database) {
                  $database->insert("ppl", ["fn"=>"joe","ln"=>"doe"])
              });

我认为您需要将其重新构建为一种 OOP 样式,这将使其更容易和模块化,但如果$database在全局范围内定义,则非常关键:

function database() {
    $GLOBALS['database']->insert("ppl", ["fn"=>"joe","ln"=>"doe"]);
}

Flight::route('/add/', 'database');
于 2016-10-05T19:24:29.883 回答