2014-03-25 37 views
1

我創建下面我發現這裏的計算器信息Opencart的幫忙,但我有一個問題:Opencart的創建和使用自定義的助手

首先,我創建了一個名爲general.php的輔助文件,放在裏面的文件夾:

system/helper/general.php 

startup.php文件然後開始

require_once(DIR_SYSTEM 'helper/general.php'); 

最後,我用它的控制器內:register.php位於文件夾catalog/controller/account/register.php內。

我用它這樣:

if (empty($this->request->post['doc']) && $this->general->validate($this->request->post['doc'])) { 
    $this->error['doc'] = 'doc is invalid'; 
} 

,並返回以下錯誤:

Fatal error: Call to a member function validate() on a non-object in/home/centralshopdistribuidora/www/vqmod/vqcache/vq2-catalog_controller_account_register.php on line 515 

行515:

if (empty($this->request->post['doc']) && $this->general->validate($this->request->post['doc'])) { 

的general.php文件:

function validate($doc) { 
    $d1 = 0; 
    $d2 = 0; 
    $doc = preg_replace("/[^0-9]/", "", $doc); 
    $ignore_list = array(
     '00000000000', 
     '', 
     '11111111111', 
     '22222222222', 
     '33333333333', 
     '44444444444', 
     '55555555555', 
     '66666666666', 
     '77777777777', 
     '88888888888', 
     '99999999999' 
    ); 
    if (strlen($doc) != 11 || in_array($doc, $ignore_list)) { 
     return false; 
    } else { 
     for ($i = 0; $i < 9; $i++) { 
      $d1 += $doc[$i] * (10 - $i); 
     } 
     $r1 = $d1 % 11; 
     $d1 = ($r1 > 1) ? (11 - $r1) : 0; 
     for ($i = 0; $i < 9; $i++) { 
      $d2 += $doc[$i] * (11 - $i); 
     } 
     $r2 = ($d2 + ($d1 * 2)) % 11; 
     $d2 = ($r2 > 1) ? (11 - $r2) : 0; 
     return (substr($doc, -2) == $d1 . $d2) ? true : false; 
    } 
} 

回答

1

您首先需要將的註冊表註冊到註冊表中,例如,在你index.php行查找

// Encryption 
$registry->set('encryption', new Encryption($config->get('config_encryption'))); 

(指OC 1.5.5或更高版本),並在此之後註冊您的幫手以同樣的方式,例如

// General 
$registry->set('general', new General())); 

這應該夠了。如果你有一些依賴關係,請務必更改構造函數調用...

+0

ty,現在可以工作。 –

+0

不客氣。 – shadyyx

相關問題