我尝试使用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 [];
}
}
我做错了什么?
答案 0 :(得分:3)
您正在使用fromQuery()
来获取ID,但ID不在查询字符串中,而是路径的一部分。你想要的是:
$id = $this->params()->fromRoute('id', 'null');