How to fetch whole data array (request, post, get) from JInput like earlier before with JRequest::get(request/post/get) ?

2,411 views
Skip to first unread message

Clubnite

unread,
Feb 6, 2014, 11:28:55 AM2/6/14
to joomla-de...@googlegroups.com
Hello,

it frequently happens that i require to have a reference to the whole data array like it could be retreived earlier when stating

$post = JRequest::get('post');

With JInput it is not possible to access this data easily. I fact i seem to have only this way:

$post = current(array_filter(unserialize(JFactory::getApplication()->input->post->serialize())));

Any recommendation how to do this the easy way is highly welcome.

Michael

unread,
Feb 6, 2014, 5:31:37 PM2/6/14
to joomla-de...@googlegroups.com
I've been using the following code, but I'm not really sure if it is the most "correct" way.

$app = JFactory::getApplication();
$postData = $app->input->getArray($_POST);

~Michael


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Michael

unread,
Mar 14, 2014, 4:00:40 PM3/14/14
to joomla-de...@googlegroups.com
In joomla 3 you can just run the following
$app = JFactory::getApplication();
$postData = $app->input->getArray(array());
This code will simply return the data var of the jinput object.

If you are in joomla 2.5 passing in an empty array in unavailable so you can use the code in my first reply. Which should have actually been
$app = JFactory::getApplication();
$postData = $app->input->getArray(array_flip(array_keys($_POST)));
The reason for the array_flip(); and array_keys(); is because otherwise it would try to use the value of each $_POST var as the type of filter to run it though.


I also wanted to follow up my previous reply because I've found this is not actually safe to use in joomla 2.5 if you are also trying to use $app->input->set(); somewhere else in your codebase. This is because $app->input->set(); only adds to the jinput data var and does not also update the superglobals unlike jrequest would. So in joomla 2.5 there would be no way to get the entire jinput data var without knowing the names of each added var. I supposed you could do something like the following every time you needed to use $app->input->set();
$app = JFactory::getApplication();
$_POST['Somevar'] = "";
$app->input->set('Somevar', 'true');
This is so the "Somevar" key will be in $_POST when you use $app->input->getArray(); But directly messing with superglobals like this kinda defeats the purpose/spirit of using jinput to begin with. 

You could also skip $app->input->set(); and just directly set it in $_POST but then that var will show up as blank when called with $app->input->getArray();. You would need to use $app->input->post->getArray(); to get the correct value in this case.

I guess the lesson here is stick to using JRequest in joomla 2.5.

~ Michael

Clubnite

unread,
Mar 14, 2014, 6:20:47 PM3/14/14
to joomla-de...@googlegroups.com
Thanks for this very informative post. I tried your suggestion and found that in Joomla! 3 it is not necessary to pass any value. In fact i could load what i wanted to load via this code:


$app      = JFactory::getApplication();
$request  = $app->input->request;
$post     = $app->input->post;
$get      = $app->input->get;
$jrequest = JRequest::get();
$jpost    = JRequest::get('post');
$jget     = JRequest::get('get');

echo '<pre>jrequest: ' . print_r($jrequest, true) . '</pre>';
echo '<pre>request: '  . print_r($request->getArray(), true) . '</pre>';
echo '<pre>jpost: '    . print_r($jpost, true) . '</pre>';
echo '<pre>post: '     . print_r($post->getArray(), true) . '</pre>';
echo '<pre>jget: '     . print_r($jget, true) . '</pre>';
echo '<pre>get: '      . print_r($get->getArray(), true) . '</pre>';
jexit();

The results are equal ... for Joomla! 3. Dunno how this behaves in J! 2.5

Michael

unread,
Mar 14, 2014, 6:35:13 PM3/14/14
to joomla-de...@googlegroups.com
Ya in joomla 3 jinput is much easier to work with, glad you got it sorted out. In joomla 2.5 when I ran something like $app->input->post I ended up getting an instance of the jinput object instead of actual data.

~ Michael


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages