【问题标题】:Execute code after every action in Symfony 1.4在 Symfony 1.4 中的每个操作之后执行代码
【发布时间】:2011-11-22 13:13:37
【问题描述】:

我知道我们可以在 Symfony 中的每个操作之前使用过滤器来创建代码,但是在每个操作完成之后呢? PostExecute 方法?

【问题讨论】:

    标签: php symfony1


    【解决方案1】:

    您也可以使用过滤器在执行后执行代码:

    class myFilter extends sfFilter {
    
        public function execute($filterChain) {
    
            // Code that is executed before the action is executed
    
            $filterChain->execute();
    
            // Code that is executed after the action has been executed
    
        }
    
    }
    

    这是因为 Symfony 中的完整执行是一个很大的“过滤器链”...如果您仔细查看您的 filters.yml,您会发现首先调用了 rendering 过滤器,然后是 security过滤器,cache 过滤器,最后是 execution 过滤器。 执行过滤器是实际执行请求的过滤器(调用控制器和一切)。

    为了说明这一点:缓存过滤器将在进入链之前检查缓存中是否有可用的有效输出,并将其返回。如果现在它将执行链中的下一个过滤器,当它返回时,存储输出,以便后续请求可以使用缓存。

    【讨论】:

      【解决方案2】:

      你必须在动作类中添加这个方法:

        public function postExecute()
        {
          // do something
        }
      

      【讨论】:

        【解决方案3】:

        postExecute 方法在每次操作调用结束时执行。
        这是documentation

        【讨论】:

        • 这适用于模块中的每个操作,而不是应用程序中的。
        猜你喜欢
        • 2014-12-05
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多