dongshicuo4844 2017-09-30 10:27
浏览 34
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douchen9569 2017-09-30 13:30
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了