【问题标题】:change body of if statement based on its condition根据条件更改 if 语句的主体
【发布时间】:2014-09-26 18:52:30
【问题描述】:

这是我的问题 我有一个类说 ABC 和在其中定义的函数 XYZ 并且该函数根据其中的上述逻辑返回一些东西。

class ABC {
function XYZ{
..........
.......
return "--- ";
}
}

现在这个类的另一个页面上有一个对象调用了这个方法。

$z= new ABC;

if( $z->XYZ() )
{
some output
}
else{
another output
}

我的问题是我无法访问第 2 页。但我需要更改 else 语句的输出。我只能访问 ABC 类,因为我在模块中覆盖它。所以,简而言之,我只能更改函数 XYZ。有什么解决办法??

如果有任何意义,我正在 MAGENTO ECOMMERCE 平台上工作,类是块类,第 2 页是模板

【问题讨论】:

  • 看来除了编辑page2没有办法。
  • @u_mulder 是的。但我不能,因为我必须进行扩展,而且每个 magento 都有不同的模板。我只能覆盖核心类,但不能覆盖主题的模板,即使我愿意。
  • 甚至模板页面也可以被覆盖,但在这种情况下我不能这样做。我希望用户页面按原样工作,除了其他部分
  • function XYZ{ if ( $this->originalXYZ() ) { some output } else { modified another output } exit(); }
  • @guest 如何在不触及模板的情况下编写修改后的另一个输出?

标签: php magento


【解决方案1】:

您可以使用 http_response_send_before 事件的观察者更改输出 html。

捕获 html 并做你的事情。

你有很好的解释here

希望对你有帮助

【讨论】:

    【解决方案2】:


    你也可以这样做:

    <?php
    ob_start();
    // do your stuff
    // ...
    
    // here you capture all the output that it is generated by your scripts
    $v = ob_get_contents();
    
    // alter the value of $v by detecting if it should be changed. you can user regex for example to 
    // detect and update
    // ...
    // here you clean all data stored in buffer
    ob_clean();
    // and put the updated data in buffer
    print $v;
    
    // end of buffer operations
    ob_end_flush();
    
    ?>
    

    希望对你有帮助,

    【讨论】:

      【解决方案3】:

      您需要一种方法来区分 PAGE1 和 PAGE2,如果可能的话,可以通过 URL 来说明。因此,您必须更改将检查当前在哪个页面上的方法,并根据它来更改输出,但是您必须将输出写入方法本身,而不是将其写入模板中。 class ABC { function XYZ { $result = $this->getResult(); //Helper method to get the result. if ($result) { //Do something when result is true. } else { $url = $this->checkUrl(); //Check URL of the page. if ($url == PAGE2) { //If you are on PAGE2 //Do something specific for the PAGE2 } else { //Do something for all other pages } } } } 我知道这不是完美的解决方案,但我希望它能以某种方式帮助您。

      【讨论】:

      • 那不行,因为“另一个输出”还是会被第二页放出来……
      • 这就是为什么我提到他需要在方法本身中编写输出并在模板中调用方法而不做if/else逻辑:$z->XYZ();
      • 但是if/else逻辑不会消失……那是他的问题!
      • 那么没有其他方法可以解决这个问题,除非通过 Block 本身或 XML 布局更改模板。
      • 你是对的。但他告诉他不能更改模板。所以我看不到解决方案...
      【解决方案4】:

      页码必须在$_GET 或$_POST 数组(或另一个查询)中指定。检查该变量并实现替代逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-06
        • 1970-01-01
        • 2017-03-30
        相关资源
        最近更新 更多