José Carlos
José Carlos

Reputation: 2922

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?

Upvotes: 0

Views: 342

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

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');

Upvotes: 3

Related Questions