【问题标题】:how to add a custom php function to all opencart pages by default默认情况下如何向所有opencart页面添加自定义php函数
【发布时间】:2015-04-23 16:44:22
【问题描述】:

我需要在 OpenCart 系统的每个页面上放置一个表单。是否有可能我可以在某个地方包含一个 PHP 函数,以便它显示在每个页面上?

类似:

<?php 
require_once('../function.php');
test(1); 
?>

我只需要知道将其放入哪个.tpl.php 文件。

我所做的是在

/system/startup.php

require_once('http://domain.com/orderform/test123.php/');

文件test123.php结构为

<?php 
require_once('../function.php');
test(1); 
?>

它工作正常,但我在网站顶部看到一行这样说

警告:session_start():无法发送会话缓存限制器 - 标头 已经发送(输出开始于 http://domain.com/orderform/test123.php/:42) 在 /home/domain/public_html/system/library/session.php 在第 12 行

【问题讨论】:

    标签: php opencart2.x


    【解决方案1】:

    我认为(未测试)你需要这个...

    您可以在 header.php 中添加功能,因此标题将在所有页面上可用,您可以在所有页面上使用您的功能

    myfunciton(){echo "hello";}
    
    
    opencart22\catalog\controller\common\header.php
    

    【讨论】:

    • 您好,我更新了问题。可以直接打开和加载功能。它工作正常,但有一些错误
    【解决方案2】:

    /catalog/controller/information/中创建文件名static.php

    // /catalog/controller/information/static.php
    function newfunction() {
      echo "I am new !!";
    }
    

    现在需要在system/startup.php 上添加该文件

    // //system/startup.php 
    require_once(DIR_SYSTEM . '/catalog/controller/information/static.php');
    

    现在您可以在任何您想要的地方直接拨打newfunction()

    【讨论】:

    • 您好,我更新了问题。可以直接打开和加载功能。它工作正常,但有一些错误
    【解决方案3】:

    system/helper 中,您可以创建自己的文件,例如other.php

    // system/helper/other.php
    function smpleFunction() {
      echo "Hi this works..!";
    }
    

    现在你需要包含,添加以下行

    // system/startup.php 
    require_once(DIR_SYSTEM . 'helper/other.php');
    

    现在您可以在任何您想要的地方直接拨打smpleFunction()

    【讨论】:

    • 您好,我更新了问题。可以直接打开和加载功能。它工作正常,但有一些错误
    【解决方案4】:

    在系统/库中,您可以创建自己的函数文件,例如functions.php

     //system/library/functions.php
     function testfuntion(){
      echo "This is test function";
     }
    

    在index.php上添加如下代码加载库functions.php

    // Cart
    $registry->set('cart', new Cart($registry));  
    
    // your library functions
    $registry->set('functions', new Functions($registry));  
    

    现在您可以通过以下代码在任何地方使用此功能。

    $this->functions->testfuntion();
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2014-04-16
      • 1970-01-01
      • 2017-01-29
      • 2021-08-30
      相关资源
      最近更新 更多