onlyoffice 回调传参数,如何在php的回调函数中传递参数?

I have Repository class with a method as follows:

public function GetOne($id){

$method = __METHOD__;

$post = null;

$post = $this->CacheManager($method, function($id) {

return DB::select("select * from posts where id = ?", [$id]);

});

return $post;

}

I want to cache the result, but in the closure/callback function the $id parameter is not working. The CacheManager is a trait where I'm using it in my repository.

public function CacheManager($method, $fn) {

$obj = null;

if(!$this->HasCache($method)){

$obj = $fn();

}else {

$obj = $this->GetCache($method);

}

return $obj;

}

I have some other methods without parameters and they're working as intended.

解决方案

Use use. :D

With the use clause, you can import variables from the parent scope into the scope of the function.

public function GetOne($id){

$method = __METHOD__;

$post = null;

$post = $this->CacheManager($method, function() use ($id) {

return DB::select("select * from posts where id = ?", [$id]);

});

return $post;

}

Just a side note. Since it looks you are building a caching mechanism, you will need to include the ID in the cache as well. Currently you only check by $method, but for each id you will probably have a different cache entry which may or may not exist. So I think in your function you need to do something like the line below to make the cache key more unique. I would also call the parameter $method something like $cacheKey instead, since to the cache it shouldn't be linked to a method name per se.

$method = __METHOD__ . ";$id";

Update for PHP 7.4: arrow functions

The RFC for arrow functions (AKA 'short closures') has passed voting.

With these you don't specify the parameters you want to close in, because they can only have a single expression anyway, so any expression/value they use can (and will) be taken from the parent function scope.

Since in this case the anonymous function just has a single statement, it can be rewritten into an arrow function. The call to the cache manager will then look like this:

public function GetOne($id){

$method = __METHOD__;

$post = null;

$post = $this->CacheManager($method, fn() => DB::select("select * from posts where id = ?", [$id]));

return $post;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于 onlyoffice 回调传参的问题,需要先了解 onlyoffice回调机制。在 onlyoffice ,当用户在文档进行编辑、保存等操作时,onlyoffice 会向指定的 URL 发送回调请求,以便通知该 URL 相关操作的结果和参。 为了让回调 URL 接收到这些参,需要在回调 URL 上定义相应的参,只有定义了这些参,才能在回调 URL 获取相应的参值。以下是一个示例回调 URL 的定义: ```php http://example.com/callback.php?status={status}&id={id}&version={version} ``` 在这个 URL ,我们定义了三个参:status、id 和 version。当 onlyoffice 发送回调请求时,会将相关参的值替换到 URL ,具体的参值根据请求的操作类型而定。例如,当用户进行保存操作时,status 参可能是 success,id 参是文档的唯一标识符,version 参是当前文档的版本号。 接下来,需要在回调 URL 对应的 PHP 文件获取这些参的值。以下是一个简单的示例: ```php $status = $_GET['status']; $id = $_GET['id']; $version = $_GET['version']; ``` 通过 $_GET['参名'] 的方式,可以获取到相应参的值,然后进行相应的处理。 注意,在实际开发,需要对回调 URL 进行安全验证,以防止恶意请求。例如,可以在回调 URL 加入一个随机字符串作为密钥,在 PHP 文件进行验证,只有在验证通过时才能处理相应的参值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值