Why is Request Object empty

about a month ago

Bill Lions
Member | 19
+
0
-

Why is the request object an giving me an empty array?
/blog/show/2

$httpRequest = $this->getHttpRequest();
$keys = array_keys( $httpRequest->query );

The route is fine, and if I manually set the response, it works, but I need to get the blog id from the query string.

Thanks, Bill

about a month ago

manwe
Member | 39
+
+2
-

Why do you need to get the ID manually?

Usually it should be Router's job to parse it and pass it to your action/render method.

You'll need to show us a bigger piece of code…

about a month ago

CZechBoY
Member | 3334
+
0
-

read this and it should be clear after reading
https://doc.nette.org/…0/presenters#…

about a month ago

Bill Lions
Member | 19
+
0
-

this is the dump from tracy, not really seeing anything here either..

Nette\Security\User #58e8
guestRole => "guest" (5)
authenticatedRole => "authenticated" (13)
onLoggedIn => null
onLoggedOut => null
storage private => Nette\Http\UserStorage #0d06
namespace private => ""
sessionHandler private => Nette\Http\Session #e086
regenerated private => true
options private => array (10) [ ... ]
request private => Nette\Http\Request #5279 { ... }
response private => Nette\Http\Response #36ac { ... }
handler private => null
sessionSection private => null
authenticator private => UserAuthenticator #bcb5
database private => Nette\Database\Context #b60b
connection private => Nette\Database\Connection #49d8 { ... }
structure private => Nette\Database\Structure #7f0f { ... }
conventions private => Nette\Database\Conventions\DiscoveredConventions #cc72 { ... }
cacheStorage private => Nette\Caching\Storages\FileStorage #5cb3 { ... }
authorizator private => Nette\Security\Permission #5595
roles private => array (5)
admin => array (2) [ ... ]
user => array (2) [ ... ]
guest => array (2) [ ... ]
john => array (2) [ ... ]
mary => array (2) [ ... ]
resources private => array (1)
backend => array (2) [ ... ]
rules private => array (2)
allResources => array (2) [ ... ]
byResource => array (1) [ ... ]
queriedRole private => null
queriedResource private => null

about a month ago

Bill Lions
Member | 19
+
0
-

Tracy also reports

/bhajan/addplaylist[/<id>]  presenter = Bhajan:Bhajan
action = addplaylist
id = null

Bhajan:Bhajan:addplaylist
id = 3

I can see initially, the request is null as a default, but then is 3 in the “Matched as” column, (as specified in the url).

Am I somehow trying to get the value before it is set? Is that even possible?

about a month ago

Bill Lions
Member | 19
+
0
-

So, on further investigation I came up with a solution that did not require the use of the request object to gain the id parameter…

 public function actionAddPlaylist( $id )
 {
        if ($this->isAjax())
        {
                $result = $this->database->table('lyrics')->get( $id );
                $title = $result->title;

                $user = $this->getUser();

                // only logged in users may create playlists
                if( $user->isLoggedIn() )
                {
                        $data = ['id'=>$id, 'title' => $title];
                        $this->sendJson($data);
                }
        }
}

There is some else conditions for the if statements, but that is the gist of it.
Is this the ‘correct/nette’ way to do this?
Passing the parameter to the action?

about a month ago

CZechBoY
Member | 3334
+
0
-

You should dump $user->getIdentity()

I dont get why you use action for ajax, use handle methods https://doc.nette.org/en/2.4/ajax

about a month ago

Bill Lions
Member | 19
+
0
-

Thanks, I was previously unaware of these handle methods.
Only been using nette for a week :)