我在哪里可以在 magento 中声明我的自定义函数
我想声明一些 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Magento 有这些方法的辅助类。因此,创建扩展并添加方法,然后您可以像下面这样调用它们
,或者您可以从常用方法中创建一个库类。
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
Or you can make a library class out of your common methods.