【问题标题】:Pass parameters to Symfony's $cache->get()将参数传递给 Symfony 的 $cache->get()
【发布时间】:2019-05-02 08:07:46
【问题描述】:

恐怕我有一个初学者 PHP 问题。我正在使用 Symfony 的缓存组件。 https://symfony.com/doc/current/components/cache.html

我在一个接收 2 个参数($url,$params)的函数内部调用缓存对象。

class MainController extends AbstractController {
    public function do($url, $params) {
        $cache = new FilesystemAdapter();
        return $cache->get('myCacheName', function (ItemInterface $c) {
            global $url;
            var_dump($url); // ---> null !!!!
        }
    }
}

我的问题是,我无法访问缓存方法调用中的函数参数。 $url 和 $params 为空。 当然,我可以使用 MainController 类中的公共类变量将变量向后发送,但这似乎有点笨拙。

【问题讨论】:

标签: php symfony symfony-cache


【解决方案1】:

在 PHP 中,默认情况下闭包无法访问其范围之外的变量,您必须 use 像这样:

return $cache->get('myCacheName', function (ItemInterface $c) use ($url) {
    var_dump($url); // ---> no longer null !!!!
}

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多