ZF3:不接受来自网址的参数

时间:2017-09-30 10:27:19

标签: php zend-framework zend-framework3

我尝试使用ZF3从url检索params时遇到问题。当我尝试从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 [];
    }

}

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您正在使用fromQuery()来获取ID,但ID不在查询字符串中,而是路径的一部分。你想要的是:

$id = $this->params()->fromRoute('id', 'null');
相关问题