Custom helper functions in OpenCart

好久不见. 提交于 2019-12-05 08:54:40

Put your helper file inside a helper folder inside a system directory

system/helper/myhelper.php

and include it to

system/startup.php file

like this

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

and you are done.

Put the function in a file eg. myhelper.php and save this to ../system/library/

Then add

require_once(DIR_SYSTEM . 'library/myhelper.php');

to ../system/startup.php

The correct and recommended way of doing this is using the OpenCart's built-in loader:

$this->load->helper('helper_name');

The helper is located in the directory system/helper. You don't need to append the php suffix when you load it, as OpenCart's loader engine appends it automatically.

And then, because the helper is not a class you use the functions directly without the $this. For example:

$this->load->helper('general');

token();

And the result will be a 32-character token. The token() function is located in the general helper in the system/helper directory.

This is an example of the general helper:

<?php
function token($length = 32) {
    // Create token to login with
    $string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    $token = '';

    for ($i = 0; $i < $length; $i++) {
        $token .= $string[mt_rand(0, strlen($string) - 1)];
    }   

    return $token;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!