3

I'm trying to build a simple joomla component what would show custom google search results based on it's API. How would I pass a get variable to a joomla component? Lets say I have already the basics what calls a custom view index.php?option=com_google&view=google, than I would like to pass 'q' $_GET variable to it how should the url query string look like ?

fefe
  • 8,755
  • 27
  • 104
  • 180

2 Answers2

6

The HTTP request method GET, works with the URL, so variables are always passed in the URL of the request.

To add q to your current URL you simply add &q=SomeValue where SomeValue has been appropriately percent or URL encoded.

Joomla 1.5

If you're using Joomla! 1.5 you can use JRequest to get the value of any variable whether submitted by POST or GET, see this document on retrieving request variable.

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

Joomla 1.6+

For Joomla! 1.6+ it is recommended to use JInput to retrieve request data as JRequest is depreciated, and for Joomla! 3.0+ you MUST use JInput as JRequest has funcitonality removed and will continue to disappear over the next few releases.

To use JInput you can either get the current object or use chaining through the current Joomla application to retrieve variables.

Getting 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...

Using JInput via chaining

$jAp = JFactory::getApplication();  // Having the Joomla application around is also useful
$q = $jAp->input->get('q');  // Your variable
Craig
  • 9,335
  • 2
  • 34
  • 38
  • 1
    Think it's just worth pointing out here for completeness that even though it's deprecated most Joomla core components still use JRequest in Joomla 2.5 as JInput has known issues with magic quotes. JRequest does not. In Joomla 3.0 magic quotes is required to be turned off therefore there are no issues there! – George Wilson Feb 05 '13 at 09:25
  • @GeorgeWilson very true, but remember the magic quotes feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. http://php.net/manual/en/security.magicquotes.php (They are also OFF by default in PHP 5+ which most hosts are using these days). – Craig Feb 05 '13 at 21:11
1

You can retrieve GET vars in Joomla using:

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

This would work for a url string as per below:

index.php?option=com_google&view=google&q=some_variable
Martin
  • 10,294
  • 11
  • 63
  • 83