我在哪里可以在 magento 中声明我的自定义函数

发布于 2024-12-09 00:59:34 字数 1568 浏览 2 评论 0原文

我想声明一些 php 函数,并且我想在 magento 的各个位置调用这些函数。通常在我的核心 php 项目中,我在functions.php 中声明 php 函数,并且在所有页面中包含该文件。我不是熟悉MVC结构。那么我在哪里可以声明这类函数呢?

谢谢

编辑:-

Mango_Myfunc.xml (app/etc/modules)

<?xml version="1.0"?>
<config>
    <modules>
        <Mango_Myfunc>
            <active>true</active>
            <codePool>local</codePool>
        </Mango_Myfunc>
    </modules>
</config>

Config.xml(app/code/local/Mango/Myfunc/etc/configure.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Mango_Myfunc>
            <version>0.1.0</version>
        </Mango_Myfunc>
    </modules>
    <global>
        <helpers>
            <Myfunc>
                <class>Mango_Myfunc_Helper</class>
            </Myfunc>
        </helpers>
    </global>
</config>

Data.php(app/code/local/Mango/Myfunc/helper/Data.php)

class Mango_Myfunc_Helper_Data extends Mage_Core_Helper_Abstract
{

public function short_str ($str, $len, $suf = '...') {
    if (strlen($str) > $len)
        return substr($str, 0, $len - strlen($suf) ) . $suf;

    return $str;
}

}

这是我添加的内容,

我使用下面的一个来调用 list.phtml 中的函数

echo $this->helper('Myfunc/Data')->short_str("test","3"); got the error

致命错误:类找不到“Mage_Myfunc_Helper_Data”

I want to declare some php functions and i would like to call those functions in various places in magento.Usually in my core php projects i'm declaring php functions in functions.php and i include that file in all pages.I'm not familiar with MVC structure.So where can i declare these kind of functions.

Thanks

Edit :-

Mango_Myfunc.xml (app/etc/modules)

<?xml version="1.0"?>
<config>
    <modules>
        <Mango_Myfunc>
            <active>true</active>
            <codePool>local</codePool>
        </Mango_Myfunc>
    </modules>
</config>

Config.xml(app/code/local/Mango/Myfunc/etc/configure.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Mango_Myfunc>
            <version>0.1.0</version>
        </Mango_Myfunc>
    </modules>
    <global>
        <helpers>
            <Myfunc>
                <class>Mango_Myfunc_Helper</class>
            </Myfunc>
        </helpers>
    </global>
</config>

Data.php(app/code/local/Mango/Myfunc/helper/Data.php)

class Mango_Myfunc_Helper_Data extends Mage_Core_Helper_Abstract
{

public function short_str ($str, $len, $suf = '...') {
    if (strlen($str) > $len)
        return substr($str, 0, $len - strlen($suf) ) . $suf;

    return $str;
}

}

This is what i added

i used bellow one to call the function in list.phtml

echo $this->helper('Myfunc/Data')->short_str("test","3"); got the error

Fatal error: Class 'Mage_Myfunc_Helper_Data' not found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2024-12-16 00:59:34

Magento 有这些方法的辅助类。因此,创建扩展并添加方法,然后您可以像下面这样调用它们

Mage::helper('yourextension/yourhelper')->yourMethod(); 

,或者您可以从常用方法中创建一个库类。

Magento has helper classes for those kind of methods. So make your extensions and add your methods and you can then later call them like follows

Mage::helper('yourextension/yourhelper')->yourMethod(); 

Or you can make a library class out of your common methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文