Joomla: Call helper function from within a model?

依然范特西╮ 提交于 2019-12-04 09:19:50

Lets break this down:

  1. In Joomla! your components helper file should be in `/mycomponent/helpers/lookup.php'

  2. JLoader:: is the Joomla! way to do it, but you could just as easily use PHP's require_once eg. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/myfunctions.php';

  3. Is your path right? - you're providing dirname(JPATH_COMPONENT_ADMINISTRATOR).DS.'helpers'.DS.'lookups.php' but you've wrapped the path to your component in dirname which will the parent element of the path only. So JLoader is looking in /administrator/helpers/lookups.php.

  4. JPATH_COMPONENT_ADMINISTRATOR is initialised as part of Joomla!'s renderComponent() call in it's JComponentHelper class if you apply dirname to it when it's not setup you will get back a dot (ie. current directory) so in the model you could would be passing ./helpers/lookups.php to the JLoader call.

You can call helper within model by following method:

JLoader::import('helpers.events', JPATH_COMPONENT);

this will include the file helpers/events.php from the component directory.

$_helper = new EventsHelper(); echo $_helper->getAnyInsideMethod();exit;

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