José Carlos José Carlos - 3 years ago 196
PHP Question

ZF3: Doesn't receive params from url

I've got a problem trying to retrieve params from url with ZF3. I have always the default value when I try to pass any value from url: http://domain/game/1

module.config.php

'game' => [
'type' => Segment::class,
'options' => [
'route' => '/game[/:id]',
'constraints' => [
'id' => '[0-9]*',
],
'defaults' => [
'controller' => Controller\GameController::class,
'action' => 'index',
],
],
],


GameController.php

class GameController extends BaseController
{

public function indexAction()
{
$log = new LogWriter();

$id = $this->params()->fromQuery('id', 'null');
$log->writeLog(get_class($this) . "::" . __FUNCTION__ . " id partido: " . $id);

return [];
}

}


What am I doing wrong?

Answer Source

You are using fromQuery() to get the id, but the id is not in the query string, it's part of the route. What you want instead is:

$id = $this->params()->fromRoute('id', 'null');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download