Joomla:在组件中编写和调用辅助函数 [英] Joomla: Write and call a helper function in a component

查看:147
本文介绍了Joomla:在组件中编写和调用辅助函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚起步的Joomla/PHP开发人员,碰壁了解如何执行此操作.我发现搜索的所有内容都是针对较旧版本的Joomla或其他框架的,因此第一次使它们都感到困惑.

我想要一个可以在组件中任何位置调用的辅助函数.基本上,它需要输入一个userID并返回其全名,比如说头发的颜色和高度.功能如下:

function get_profile_info($userID) {

        $db =& JFactory::getDBO();          
        $query = $db->getQuery(true);

        $query->SELECT('u.id as UserID
                    , u.name AS Name
                    , up.profile_value AS Hair
                    , up2.profile_value AS Height
                    ');
        $query->FROM (' #__users AS u');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
        $query->WHERE(' u.id = '.$userID.'');
        $query->GROUPBY (' u.id
                    , u.name
                    , up.profile_value
                    ');


       $db->setQuery($query);   
       return $query;                   
}

我将其放在组件的"helpers"文件夹中的一个名为"lookups.php"的文件中...但是在这里,我不确定下一步该怎么做. lookups.php的顶部具有强制性:

<?php defined ( '_JEXEC' ) or die;

所以有两个问题: 1)我是否将所有内容放在一个类中或将其保留为一系列功能(因为会有其他功能)?

2)如何传递用户ID并在视图(view.html.php/default.php)中获取Name,Hair和Height的值?

谢谢!

=================================

基于@Shaz在下面的响应,这就是我的位置(再次,刚开始并尝试将我的头缠在其中):

lookups.php

abstract class LookupHelper {

        var $Name;
        var $Hair;
        var $Height;

    public function get_profile_info($userID) {
                ... 
                (same as above until the next line)

                $db->setQuery($query);
                $result=$db->loadRow();
                $Name = $result[1];
                $Hair = $result[2];
                $Height = $result[3];
        $getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
                $return $getprofileinfo;
           }
    }

然后在我的default.php中(可能会移至view.html.php):

    $getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
    $getprofileinfo = LookupHelper::get_profile_info($userID);

    $Name = $getprofileinfo[Name];
    $Hair = $getprofileinfo[Hair];
    $Height = $getprofileinfo[Height];

所以...它正在工作-但似乎有一种更简单的方法(特别是手动创建数组然后按位置调用).有想法吗?

谢谢!

解决方案

  1. 创建助手类,并在其中包含所有功能:

    抽象类HelloWorldHelper { /* 功能 */ }

  2. 调用函数并存储结果:

    $ var = HelloWorldHelper :: get_profile_info($ thatId);

Fledgling Joomla / PHP developer, hitting a wall understanding how to do this. Everything I found searching has been for older versions of Joomla or other frameworks and so it's all confusing the first time around.

I want to have a helper function that I can call from anywhere in my component. Basically it takes a userID input and returns their full name, let's say hair color and height. Here's the function:

function get_profile_info($userID) {

        $db =& JFactory::getDBO();          
        $query = $db->getQuery(true);

        $query->SELECT('u.id as UserID
                    , u.name AS Name
                    , up.profile_value AS Hair
                    , up2.profile_value AS Height
                    ');
        $query->FROM (' #__users AS u');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
        $query->WHERE(' u.id = '.$userID.'');
        $query->GROUPBY (' u.id
                    , u.name
                    , up.profile_value
                    ');


       $db->setQuery($query);   
       return $query;                   
}

I'd put this in a file called "lookups.php" within the "helpers" folder of my component...but here's where I'm not sure what to do next. Top of lookups.php has the obligatory:

<?php defined ( '_JEXEC' ) or die;

So two questions: 1) Do I put everything in a class or keep it as a series of functions (since there will be others)?

2) How do I pass the userID and get back the values of Name, Hair, and Height in the view (view.html.php / default.php)?

Thank you!

==================================

Edit: Based on @Shaz 's response below here's where I'm at (again, just starting off and trying to wrap my head around some of this):

lookups.php

abstract class LookupHelper {

        var $Name;
        var $Hair;
        var $Height;

    public function get_profile_info($userID) {
                ... 
                (same as above until the next line)

                $db->setQuery($query);
                $result=$db->loadRow();
                $Name = $result[1];
                $Hair = $result[2];
                $Height = $result[3];
        $getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
                $return $getprofileinfo;
           }
    }

Then in my default.php (will probably move to view.html.php):

    $getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
    $getprofileinfo = LookupHelper::get_profile_info($userID);

    $Name = $getprofileinfo[Name];
    $Hair = $getprofileinfo[Hair];
    $Height = $getprofileinfo[Height];

So...it's working - but it seems like there's a much easier way of doing this (specifically manually creating an array then recalling by position). Thoughts?

Thank you!!

解决方案

  1. Create the helper class and include there all your functions:

    abstract class HelloWorldHelper { /* functions */ }

  2. Call the function and store the result:

    $var = HelloWorldHelper::get_profile_info($thatId);

这篇关于Joomla:在组件中编写和调用辅助函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆