视图无法调用控制器功能 Zend Framework 2

View can't call Controller Function Zend Framework 2

我无法在视图页面中调用我的控制器。即使我在控制器中使用 print_r 但它没有显示。我有 body_product.phtml 查看代码:

<table class="table table-hover table-condensed">
                <tbody>
                    <?php
                        $i = 1;
                        //print_r("list produk:".$this->productList);
                         foreach ($this->productList as $data) {
                            $desc=explode(",",$data['descriptions']);
                            ?>
                            <tr>
                                <th colspan="3">
                                    <input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
                                    <input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
                                    <h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
                                </th>
                            </tr>
                            <tr id="dashe">
                                <td>
                                    <ul class="myul">
                                    <?php foreach($desc as $descriptions) { ?>
                                        <li class="myli"> <?php echo $descriptions; ?></li>
                                    <?php } ?>
                                    </ul>
                                </td>
                                <td>
                                    <h4 class="prize">
                                        <?php setlocale(LC_MONETARY, 'id_ID');
                                        echo money_format('%.2n', $data['package_price']); ?>
                                        / month
                                    </h4>
                                </td>
                                <td>
                                    <p id="btn-hrm" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
                                </td>
                            </tr>
                            <?php
                            $i++;
                        }
                    ?>
                </tbody>
            </table>

并在 indexController 中:

 public function loadProductAction() {
        $viewModel = new ViewModel();
        $storage = Product\Storage::factory($this->getDb());
        $productList = new Product($storage);
        $data = $productList->loadProduct();
        $arr = array();
        if ($data) {
            foreach ($data as $val) {
                array_push($arr, $val);
            }
        }
        print_r('teaaat'.$arr);
        $viewModel->setVariables(array('productList' => $arr))
                ->setTerminal(true);
        return $viewModel;
    }

如果我在视图中打开 print_r,它会显示错误 Warning: Invalid argument supplied for foreach() in...。我认为它导致视图无法调用控制器。 请帮助我,谢谢。

首先,当您尝试使用 print_r 时,它需要一个数组。所以它应该类似于 print_r($arr)。也试试这个,看看它是否有帮助。

public function loadProductAction() {
        $storage = Product\Storage::factory($this->getDb());
        $productList = new Product($storage);
        $data = $productList->loadProduct();
        $arr = array();
        if (is_array($data) && !empty($data)) {
            foreach ($data as $val) {
                array_push($arr, $val);
            }
        } else {
            echo '$data is not an array or it is empty';
        }
        print_r($arr);
        return new ViewModel(array(
            'productList' => $arr
        ));
    }