URL : getQueryParameters doesn't work

Notice: This thread is very old.
Reka
Member | 19
+
0
-

Hello,

My link takes this form :
http://localhost/nette/www/subscription/confirmation/id=67&conf=565185338

To generate that URL, I used the function “setQuery” with success :

	$confirm_parameters = [array];
	$url = $this->httpRequest->getUrl()->setQuery($confirm_parameters);

Conversely, I am unable to get the parameters I set (in the same presenter) :

Below, you will find my several attempts and their results :

dump($this->httpRequest->getUrl()->getPathInfo());

Result :
"subscription/confirmation/id=67&conf=565a315c05338" (50)

dump($this->httpRequest->getUrl()->getQueryParameters());

Result :
array () [empty]

dump($this->httpRequest->getUrl()->getQuery());

Result :
"" [nothing]

I read this page and this one at least 20 times, but apparently sth goes wrong :/

Could you show me how to catch the values of “id” and “conf” which appear in my link, please?

Thank you in advance and have a nice weekend.

Last edited by Reka (2015-11-29 00:37)

CZechBoY
Member | 3598
+
0
-
class SubscribePresenter
{
    public function actionConfirmation ($id, $conf)
    {
        ...
    }
}

Write action method with parameters with same name as in url.

duke
Member | 650
+
+1
-

You have missing ? in the url.

Reka
Member | 19
+
0
-

Such a responsiveness !
Thank you for this help.

With the $id and $conf parameters as arguments of my method “actionConfirmation”, I still get an array empty, and then with the “?” in the URL, I get this error :
Possible problem: you are sending a HTTP header while already having some data in output buffer. Try Tracy\OutputDebugger or start session earlier.

Do I forget something else ? Do I make another mistake ?
What am I supposed to do ?

Edit : is the “?” necessary? I see the common url display the page's extension (.php) :
page.php?param1=valeur1&param2=valeur2&param3=valeur3&param4=valeur4
Is it impossible to catch the args with a path like mine? /confirmation/id=...&conf=...
If it is, how is it possible to generate this kind of url (.php?param=...) with Nette?

Last edited by Reka (2015-11-29 02:05)

CZechBoY
Member | 3598
+
0
-

You want to generate link to presenter.
In presenter:

$this->link('Subscribe:confirmation', ['id' => 123, 'conf' => 'abcdef']);

In Latte:

<a href="{plink Subscribe:confirmation, id => 123, conf => abcdef}">Confirmation</a>

Last edited by CZechBoY (2015-11-29 12:16)

Jan Tvrdík
Nette guru | 2565
+
0
-

Nette\Http\Request is immutable (you can't change it once it has been created); getUrl() returns clone of the URL

Reka
Member | 19
+
0
-

Ok thanks a lot, I understand.
I will try the solution purposed by Czechboy, I haven't generated my URL like this at all(1). It was undeniably an error.

(1)

		$url = $this->httpRequest->getUrl()->setQuery($confirm_parameters);
		$query = $url->query;
		$mail->setFrom('Blabla <mail@domain.com>')
		->addTo($senddata->email)
		->setSubject('Site : subscription')
		->setHTMLBody(
			'<h1>Join our site</h1>
			<p>To activate your account, click there :</p>
			<p><a href="http://localhost/nette/www/subscription/confirmation/'.
			$query.'" target="_blank">Confirmation.</a></p>

I will come back to you after some tests to say if it succeeds. I hope it will work ! :)

Reka
Member | 19
+
0
-

It still not work.

	public function renderOther($id, $conf) { // when I try to use actionConfirmation, the output buffer error comes back
		$id = 152;
		$rand = mt_rand();
		$link = $this->link('Subscription:confirmation', ['id' => $id, 'conf' => $rand]);
		dump($link);
		$link2 = $this->httpRequest->getUrl()->getQueryParameter($conf);
		$link3 = $this->httpRequest->getUrl()->getQueryParameters();
		dump($link2);
		dump($link3);
	}

Observation :
with this code,
$link → “/nette/www/subscription/confirmation/152?conf=1811377368” (56)
$link2 & $link3 → array ()

when I change the 4th line and put ‘ids’=>$id,
$link → “/nette/www/subscription/confirmation?ids=152&conf=378211100” (59)
but… $link2 / $link3 → array ()

Jan Tvrdík has maybe given an obvious indicator of my mistake, but I am unsure to understand it.

Last edited by Reka (2015-11-29 15:59)

Šaman
Member | 2525
+
0
-

Function getQueryParameters() has no parameter and returns an array.

Reka
Member | 19
+
0
-

Ok, I didn't see the difference between getQueryParameters() and getQueryParameter() (I corrected my code above as a consequence)
But it doesn't give an empty array, is it?
There are two args in the URL which are passed to the method too.
If I don't use getQueryParameter(s) to get the parameters in the URL, what am I supposed to use as function?

Last edited by Reka (2015-11-29 16:06)

Šaman
Member | 2525
+
+1
-

Reka wrote:

But it doesn't give an empty array, is it?
There are two args in the URL which are passed to the method too…

Are you sure? Are that two parameters in URL? That mean in address bar of browser?
You create a link with parameters in (1).
Then you dont create any link, just variable named $link2 where are your actual request conf param. Same as in $link2, there is actual request parameters.

CZechBoY
Member | 3598
+
0
-

What about $link? Isn't it correct? Or what is your expected result of link?

Reka
Member | 19
+
0
-

Ok, I understand… Saman was right !
My last tests simulated the URL inside the code and the page but I didn't integrate the URL in the address bar…
The result is now OK :
array (2)
ids ⇒ “152” (3)
conf ⇒ “853349280” (9)

A last question : how do you explain the difference of behaviors between id(1) and ids(2) as args ?
(1) → “/nette/www/subscription/confirmation/152?conf=1811377368” (56)
(2) → “/nette/www/subscription/confirmation?ids=152&conf=378211100” (59)

Is it a security system… ?

Thank you again, everybody.

Last edited by Reka (2015-11-29 16:56)

David Matějka
Moderator | 6340
+
0
-

@Reka id parameter is defined in the route mask: https://github.com/…rFactory.php#L19

Reka
Member | 19
+
0
-

Ok, nice.
The penny has dropped :)
Thank you, David.