我正在尝试构建一个简单的Joomla组件,根据Google API显示自定义谷歌搜索结果。如何将get变量传递给Joomla组件?假设我已经有了基础知识,调用一个自定义视图index.php?option=com_google&view=google
,那么我想将'q' $_GET
变量传递给它,URL查询字符串应该是什么样子的?
我正在尝试构建一个简单的Joomla组件,根据Google API显示自定义谷歌搜索结果。如何将get变量传递给Joomla组件?假设我已经有了基础知识,调用一个自定义视图index.php?option=com_google&view=google
,那么我想将'q' $_GET
变量传递给它,URL查询字符串应该是什么样子的?
HTTP请求方法GET
使用URL,因此变量始终通过请求的URL传递。
要将q
添加到当前URL中,只需添加&q=SomeValue
,其中SomeValue
已被适当百分号或URL编码。
Joomla 1.5
如果您正在使用Joomla!1.5,则可以使用{{link3:JRequest
}}获取任何变量的值,无论是通过POST
还是GET
提交的,请参阅检索请求变量的此文档。
$q = JRequest::getVar('q');
Joomla 1.6+
对于Joomla!1.6+,建议使用JInput
检索请求数据,因为JRequest
已经过时,而且对于Joomla!3.0+,你必须使用JInput
,因为JRequest
的功能被删除,并将在接下来的几个版本中继续消失。
要使用JInput
,您可以获取当前对象或通过当前Joomla应用程序链接来检索变量。
获取JInput
$jAp = JFactory::getApplication(); // Having the Joomla application around is also useful
$jInput = $jAp->input; // This is the input object
$q = $jInput->get('q'); // Your variable, of course get() support other option...
JInput
。$jAp = JFactory::getApplication(); // Having the Joomla application around is also useful
$q = $jAp->input->get('q'); // Your variable
您可以使用以下代码在Joomla中检索GET变量:
$q = JRequest::getVar( 'q' );
index.php?option=com_google&view=google&q=some_variable