View无法调用Controller Function Zend Framework 2

View无法调用Controller Function Zend Framework 2

问题描述:

I can't call my controller in view page. even if i use print_r in controller but it didn't show. I have body_product.phtml view code:

<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>

and in the 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;
    }

If I open print_r in the view,it show error Warning: Invalid argument supplied for foreach() in.... I think it cause of view can't call the controller. Help me please,thanks.

Firstly when you try to use print_r it is expecting an array. So it should be something like print_r($arr). Also give this a try and see if it helps.

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
        ));
    }