ZF3:没有收到来自 url 的参数
ZF3: Doesn't receive params from url
我在尝试使用 ZF3 从 url 检索参数时遇到问题。当我尝试从 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 [];
}
}
我做错了什么?
您正在使用 fromQuery()
获取 ID,但 ID 不在查询字符串中,它是路由的一部分。你想要的是:
$id = $this->params()->fromRoute('id', 'null');
我在尝试使用 ZF3 从 url 检索参数时遇到问题。当我尝试从 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 [];
}
}
我做错了什么?
您正在使用 fromQuery()
获取 ID,但 ID 不在查询字符串中,它是路由的一部分。你想要的是:
$id = $this->params()->fromRoute('id', 'null');