php 如何获取组件参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10431995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:09:11  来源:igfitidea点击:

How to get component parameters?

phpjoomlajoomla2.5joomla-extensions

提问by Nico

I have a problem here and just cant solve it :-/

我在这里有一个问题,只是无法解决它:-/

I am developing an Joomla component with backend. In the backend I set a parameter, the dashboardId, but I can't access them in the view. I always get data:protected(when I dump params). It seems like I'm not allowed to access the object.

我正在开发一个带有后端的 Joomla 组件。在后端,我设置了一个参数 the dashboardId,但我无法在视图中访问它们。我总是得到data:protected(当我倾倒时params)。似乎不允许我访问该对象。

Here is the default.xml:

这是default.xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="Dashboard">
        <message>
            <![CDATA[dashboard LAYOUT DESCRIPTION]]>
        </message>
    </layout>
    <fields name="params">
        <fieldset name="params">
            <field
                name="dashboardId" 
                type="text" 
                label="Dashboard ID"
                description="com_dashboard_desc"
                default="1"
            >   
            </field>
        </fieldset>
    </fields>
</metadata>

Now, in the view.html.phpI try to access the parameter like this:

现在,view.html.php我尝试像这样访问参数:

$app = &JFactory::getApplication();
$params = JComponentHelper::getParams('com_dashboard');
$dashboardId = $params->get('dashboardId');
var_dump($dashboardId);

When I do var_dump($dashboardId);I get NULLbut when I dump $app, I can see the dashboardID

当我这样做时,var_dump($dashboardId);我得到NULL但当我倾倒时$app,我可以看到dashboardID

every help would be appreciated! Thanks

每一个帮助将不胜感激!谢谢

回答by Lobo-X

There's a more simple way. First import Joomla Component Helper:

还有一个更简单的方法。首先导入Joomla Component Helper:

jimport('joomla.application.component.helper'); // not required in Joomla 3.x

And then retrieve any attribute you want like this:

然后像这样检索你想要的任何属性:

$params = JComponentHelper::getParams('com_dashboard');
$dashboardID = $params->get('dashboardID');

Greetings.

你好。

回答by Bernardo Siu

$app = JFactory::getApplication('site');
$componentParams = $app->getParams('com_example');
$param = $componentParams->get('paramName', defaultValue);

回答by J.T. Blum

Similar to the answer provided by LoboX, I'd recommend using the component helper to get component parameters:

与 LoboX 提供的答案类似,我建议使用组件助手来获取组件参数:

jimport('joomla.application.component.helper'); // Import component helper library
$params = JComponentHelper::getParams(JRequest::getVar('option')); // Get parameter helper (corrected 'JRquest' spelling)
$params->get('parameter_name'); // Get an individual parameter

The JRequest::getVar('option')returns your component's name with the com_ prefix. Aside from that, it looks like you're trying to mix a little bit of 1.5/1.6 syntax into your configuration file. If you haven't seen it yet, try reading through the 2.5 versionof the documentation. I hope that helps!

JRequest::getVar('option')返回的组件的名称与COM_前缀。除此之外,看起来您正在尝试将一些 1.5/1.6 语法混合到您的配置文件中。如果您还没有看过,请尝试通读2.5 版本的文档。我希望这有帮助!

回答by aspirisen

It is simular to J.T. Blum answer, but in Joomla 3.x JRequest is depricated. It is another way to get the apllication's params.

它类似于 JT Blum 的回答,但在 Joomla 3.x JRequest 中被弃用。这是获取应用程序参数的另一种方法。

    $app = JFactory::getApplication();
    $input = $app ->input;
    $componentName = $input ->get('option');
    $params = JComponentHelper::getParams($componentName);
    print_r($params);

回答by Stergios Zg.

Helper Function to get Params Object in all Joomla Versions 1.5 / 2.5 /3.x

在所有 Joomla 版本 1.5 / 2.5 / 3.x 中获取参数对象的辅助函数

class myCompoHelper{

    public static function getParams($option)
    {

        if (version_compare(JVERSION, '1.5', 'g'))
        {
            $application = JFactory::getApplication();
            if ($application->isSite())
            {
                $params = $application->getParams($option);
            }
            else
            {
                jimport('joomla.application.component.helper');
                $params = JComponentHelper::getParams($option);
            }
        }
        else
        {
            jimport('joomla.application.component.helper');
            $params = JComponentHelper::getParams($option);
        }
        return $params;
    }

}

$params=myCompoHelper::getParams('com_mycomponent');
echo $params->get('myParamName',null);

回答by Sebastian Barth

I had a similar problem. I only got the data:protectedresult until I went to the configuration of my component and saved it. Though there were default values printed in the textfields Joomla wasn't able to read them before clicking on 'save'.

我有一个类似的问题。我只得到data:protected结果,直到我去配置我的组件并保存它。尽管在文本字段中打印了默认值,但在单击“保存”之前,Joomla 无法读取它们。

回答by Paulo Griiettner

Since version 3.1 Joomla is in process to deprecate all Jclasses, matter effect, version 4.0 will deprecate almost all Jclasses, the recommended way from now on to retrieve components param is either calling the entire namespace function:

由于 3.1 版 Joomla 正在弃用所有J类,重要的是,4.0 版将弃用几乎所有J类,从现在起检索组件参数的推荐方法是调用整个命名空间函数:

Joomla\CMS\Component\ComponentHelper::getParams('com_dashboard')->get('dashboardID');

or, if you are working on a model, you can call usekeyword in order to import the file and use the class down in the document, like

或者,如果您正在处理模型,则可以调用use关键字以导入文件并在文档中使用该类,例如

use Joomla\CMS\Component\ComponentHelper;

function myFunction() {
    $param = ComponentHelper::getParams('com_dashboard');
    $dashboardID = $param->get('dashboardID');
}

回答by themhz

I had the same problem and the solution was this:

我遇到了同样的问题,解决方案是这样的:

$input = JFactory::getApplication()->input;
$parametername = $input->getInt('parametername');
echo $parametername;

This is the name of a parameter in the default.xml in the views tmpl folder. here is the xml

这是 views tmpl 文件夹中 default.xml 中参数的名称。这是xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="Offers">
         <message>This is a component in order to display offers</message>
    </layout>
    <state>
        <name>name</name>
            <description>description</description>

        <url addpath="/administrator/components/com_component/elements">
            <param name="category_id" section="com_component" type="categoriesmenu"  value_field="category_name" label="COM_COMPONENT_CATEGORY_FIELD_SELECT_TITLE" description="COM_COMPONENT_CATEGORY_FIELD_SELECT_TITLE_DESC" />
        </url>
    </state>

    <fields name="request" >
        <fieldset name="request" addfieldpath="/administrator/components/com_component/models/fields">
            <field name="parametername" type="category"
                label="category"
                required="true"
                description="Perigrafh"
            />
        </fieldset>
    </fields>


</metadata>