【问题标题】:passing get variables in joomla component在 joomla 组件中传递获取变量
【发布时间】:2013-01-19 14:29:06
【问题描述】:

我正在尝试构建一个简单的 joomla 组件,该组件将根据其 API 显示自定义谷歌搜索结果。如何将 get 变量传递给 joomla 组件?假设我已经具备了所谓的自定义视图 index.php?option=com_google&view=google 的基础知识,而不是我想将 'q' $_GET 变量传递给它 url 查询字符串应该如何?

【问题讨论】:

    标签: variables joomla get components


    【解决方案1】:

    HTTP request method GET 与 URL 一起使用,因此变量总是在请求的 URL 中传递。

    要将q 添加到您当前的网址,您只需添加&q=SomeValue,其中SomeValue 已适当地为percent or URL encoded.

    Joomla 1.5

    如果您使用的是 Joomla! 1.5 可以使用JRequest 获取任何变量的值,无论是POST 还是GET 提交的,参见this document on retrieving request variable

    $q = JRequest::getVar('q');
    

    Joomla 1.6+

    对于 Joomla! 1.6+ 推荐use JInput to retrieve request data 因为JRequest 已贬值,而对于Joomla! 3.0+ 你必须使用JInput 作为JRequest has funcitonality removed 并且将在接下来的几个版本中继续消失。

    要使用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 核心组件仍然在 Joomla 2.5 中使用 JRequest,因为 JInput 已经知道魔术引号的问题。 JRequest 没有。在 Joomla 3.0 中,魔术引号需要关闭,因此没有问题!
    • @GeorgeWilson 非常正确,但请记住魔术引号功能自 PHP 5.3.0 起已弃用,自 PHP 5.4.0 起已删除。 php.net/manual/en/security.magicquotes.php(它们在 PHP 5+ 中也默认关闭,现在大多数主机都在使用)。
    【解决方案2】:

    您可以使用以下方法在 Joomla 中检索 GET 变量:

    $q = JRequest::getVar( 'q' );
    

    这适用于如下所示的 url 字符串:

    index.php?option=com_google&view=google&q=some_variable
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 2015-12-13
      相关资源
      最近更新 更多