CakePHP 中的值未从 ctp 文件传递​​到控制器

Values not passed from ctp file to controller in CakePHP

我已经尝试了在这个论坛和其他论坛上发布的几种解决方案,但到目前为止都没有帮助。所以我终于发布了我的问题。顺便说一句,我正在使用 CakePHP 3.6。

我正在尝试通过 view.ctp 中的提交按钮将变量 ($product->id) 传递到我的控制器操作 "addit",但我只得到 "Undefined variable: id "(我试过了addit($id)addit() 这两种情况我都得到相同的结果。)

view.ctp

<p>
    <?php echo $this->Form->create('NULL',['url'=>['controller'=>'products','action'=>'addit']]);?>
    <?php echo $this->Form->input('id', ['type' => 'hidden', 'value' => $product->id]); ?>

    <?php echo $this->Form->button('Add to cart now');?>

    <?php echo $this->Form->end();?>

</p>

控制器:产品

public function addit() {
    $this->autoRender = false;
    if ($this->request->is('post')) {
        // $this->Products->addProduct($this->request->data['Cart']['product_id']);
        echo "".$this->Products->get($id);//for test
    } else {
        echo "".$this->Products->get($id);//for test
    }
 }

这是你想做的吗?

$id = $this->request->getData('id');
debug($this->Products->get($id)); //for test

根据 Cakephp 3.6

All POST data can be accessed using Cake\Http\ServerRequest::getData(). Any form data that contains a data prefix will have that data prefix removed. For example:

// An input with a name attribute equal to 'MyModel[title]' is accessible at
$title = $this->request->getData('MyModel.title');

您可以像这样获取 $id 变量的值:

$id = $this->request->getData('id');

进一步阅读:Request Body Data