1

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

我正在尝试通过$product->id提交按钮将view.ctp变量addit($id)addit()

视图.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
    }
 }
4

2 回答 2

1

根据 Cakephp 3.6

所有 POST 数据都可以使用 Cake\Http\ServerRequest::getData() 访问。任何包含数据前缀的表单数据都将删除该数据前缀。例如:

// 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');

进一步阅读:请求正文数据

于 2018-09-30T05:38:40.403 回答
0

这是你想做的吗?

$id = $this->request->getData('id');
debug($this->Products->get($id)); //for test
于 2018-09-29T16:03:33.383 回答