Joomla 从组件传递值以在插件和模块中使用

2022-01-06 00:00:00 php joomla2.5 joomla

如何从组件传递一个值或在某个页面上设置某个值,然后它可以在 joomla 的插件或模块中使用?

How can I pass a value from component or set the certain value on a certain page that it then could be use in the plugin or module of joomla?

例如,在此视图中:index.php?option=com_mycom&view=myview 将设置 $a = 1(它可以从数据库生成).

For example, in this view: index.php?option=com_mycom&view=myview will set $a = 1 (it could be generated from database).

每次人们来到这个地址时,都会有一个变量$a = 1,我可以在我的插件中使用它.

Every time people come to this address there will be a variable $a = 1 that I could use it in my plugin.

我知道有些人建议使用 session,但这对我来说不是解决这个问题的好方法.

I know some people suggest using session but it is not a good way for me to solve this problem.

推荐答案

老问题,我知道,但这里没有答案,事情从上面的原始评论略有变化.JRequest 现在已在 Joomla 的最新版本中弃用.

Old question, I know, but there are no answers here, and things have moved on slightly from the original comment above. JRequest is now deprecated in recent versions of Joomla.

目前的做法是:

在您的组件代码中:

$app = JFactory::getApplication();
$app->input->set('myvar', 123);

在您的模块/插件代码中:

In your module/plugin code:

$app = JFactory::getApplication();
$app->input->get('myvar');

如您所说,使用会话对象是实现此目的的另一种方法,但它的明显缺点是在 Web 请求之间持久化,这通常不是您想要的.

As you say, using the session object is another way to do this, but it has the obvious downside of being persisted between web requests, which is often not what you want.

相关文章