【问题标题】:Phalcon request with custom filter带有自定义过滤器的 Phalcon 请求
【发布时间】:2020-08-07 15:14:49
【问题描述】:

我正在使用 phalcon 3 并尝试覆盖默认的 httprequest 类并添加默认的自定义过滤器,如下所示:

public function getPut($name = null, $filters = null, $defaultValue = null, $notAllowEmpty = false, $noRecursive = false)
{
     return parent::getPut($name, [$filters, function($value){return str_replace(['null', 'undefined'], null, $value);}], $defaultValue, $notAllowEmpty, $noRecursive);
}

这样我不必在我使用 request->getPut('paramname') 的每个地方(控制器)添加过滤器。但它不起作用。有人知道怎么做吗?

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

    如果$filters 应该是一个函数数组,您需要将$filters 默认设置为一个空数组,并将您的自定义过滤器添加到该参数:

    public function getPut($name = null, $filters = [], $defaultValue = null, $notAllowEmpty = false, $noRecursive = false)
    {
         $filters[] = function($value){return str_replace(['null', 'undefined'], null, $value);};
         return parent::getPut($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
    }
    

    【讨论】:

    • 谢谢你的回答,你说得对,如果它应该是一个函数数组,你的方式应该可行。但问题的重点更像是如果。我知道它可以接受一个字符串数组,因为框架有一些内置的过滤器,比如“trim”,但我不知道如何向该列表添加更多内容。所以我需要更熟悉 phalcon 内部工作原理的人的帮助。
    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2018-05-25
    • 2016-07-10
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多