在Flight PHP上将变量传递给obejct方法
根据,使用对象方法是通过使用:在Flight PHP上将变量传递给obejct方法,php,rest,flightphp,Php,Rest,Flightphp,根据,使用对象方法是通过使用: Flight::route('/some/route', [$object, 'method']); Flight::route('/@name/@id', function($name, $id){ echo "hello, $name ($id)!"; }); 要使用路线参数,请使用: Flight::route('/some/route', [$object, 'method']); Flight::route('/@name/@id', fu
Flight::route('/some/route', [$object, 'method']);
Flight::route('/@name/@id', function($name, $id){
echo "hello, $name ($id)!";
});
要使用路线参数,请使用:
Flight::route('/some/route', [$object, 'method']);
Flight::route('/@name/@id', function($name, $id){
echo "hello, $name ($id)!";
});
我试着这样把两者结合起来:
Flight::route('/user/@id', [$object, 'method']);
但它不起作用。有没有办法将参数传递给对象方法?在闭包中分配变量如何
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
});
在闭包中分配变量怎么样
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
});
查看(方法
callFunction
和invokeMethod
),您的用例应该是受支持的。匿名函数和类方法中应该同样支持参数…看看(方法callFunction
和invokeMethod
),您的用例应该得到支持。匿名函数和类方法中应该同样支持参数…这段代码适合我:
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,但这是一种回调:
此代码适用于我:
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,但这是一种回调: