1

根据Flight PHP 文档,使用对象方法是通过使用:

Flight::route('/some/route', [$object, 'method']);

并使用路由参数是通过使用:

Flight::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";
});

我试图将两者结合起来:

Flight::route('/user/@id', [$object, 'method']);

但它不起作用。有没有办法将参数传递给对象方法?

4

3 回答 3

0

如何在闭包中分配变量?

Flight::route('/@name/@id', function($name, $id){
    $obj = new Object; // or use a DIC
    $obj->name = $name;
    $obj->id = $id; // or assign these in the constructor
});
于 2017-08-17T14:59:58.240 回答
0

这段代码对我有用:

function someFunction($id) {
  echo 'id: ' . $id;
}

class SomeClass {
    function method1($id) {
      echo 'Class, id: ' . $id;
    }
    function method2($name, $id) {
      echo 'Class, name: ' . $name . ', id: ' . $id;
    }
}
$object = new SomeClass();

Flight::route('/user/@id', array($object, 'method1'));
Flight::route('/user/@id/@name', array($object, 'method2'));
Flight::route('/fun/@id', 'someFunction');

我不擅长 PHP,但它与回调有关: https ://www.exakat.io/the-art-of-php-callback/

于 2019-03-15T00:04:04.790 回答
0

查看Dispatcher.php(方法callFunctioninvokeMethod),您的用例应该是受支持的。在匿名函数和类方法中,参数应该得到同样良好的支持......

于 2018-01-02T02:32:28.140 回答