注意:未定义的变量:zendframework 3中的pic

时间:2018-12-09 10:22:11

标签: php doctrine zend-framework3

这是Crudcontroller
             

            /**
             * This controller is designed for managing image file uploads.
             */
            class CrudController extends AbstractActionController 
            {
               /**
               * Entity manager.
               * @var Doctrine\ORM\EntityManager
               */
              private $entityManager;
              /**
                 * Image manager.
                 * @var Application\Service\PicManager;
                 */
              private $picManager;

                /**
                 * Constructor.
                 */
                public function __construct($entityManager, $picManager)
                {
                    $this->entityManager = $entityManager;
                    $this->picManager = $picManager;
                }

                /**
                 * This is the default "index" action of the controller. It displays the 
                 * Image Gallery page which contains the list of uploaded images.
                 */
                public function indexAction() 
                {
                    //$form = new AddForm();
                    //return new ViewModel(['form' => $form]);
                    // Get the list of already saved files.
            //$files = $this->picManager->getSavedFiles();
            $pic = $this->entityManager->getRepository(Pic::class)->findAll();
            return new ViewModel(['pic' => $pic]);
             }

                /**
                 * This action shows the image upload form. This page allows to upload 
                 * a single file.
                 */
                public function uploadAction() 
                {
                    // Create the form.
                    $form = new AddForm();
                    // Check whether this post is a POST request.
                    if ($this->getRequest()->isPost()) {
                        // Get POST data.
                        $data = $this->params()->fromPost();
                        // Fill form with data.
                        $form->setData($data);
                        if ($form->isValid()) {
                            // Get validated form data.
                            $data = $form->getData();}

                            // Use post manager service to add new post to database. 
                         $img_name = $data['file']['name'];
                         $url = $_SERVER['HTTP_SERVER'];
                         $seg= explode('/',$url);
                         $path = $seg[0].'/'.$seg[1].'/'.$seg[2].'/'.$seg[3];
                         $full_url = $path.'/'.'public'.'/'.'img'.'/'.$img_name;
                         $data['file']['tmp_name'] = $full_url;
                         $value = [
                      'name' => $data['file']['name'],
                      'avatar' => $data['file']['tmp_name'],
                                ];
                          $this->picManager->addNewImage($data);
                           return $this->redirect()->toUrl('http://projecting.localhost/images');
                    }
                return new ViewModel([
                        'form' => $form
                    ]);
                }

            }

这是我的upload.phtml视图文件

        <?php
        $this->headTitle('Upload a New Image');
        $form = $this->form;
        //$form->setAttribute('action',$this->url('crud',['action' => 'add']));

        //$form->get('submit')->setAttributes(['class'=>'btn btn-primary']);
        $form->prepare();
        ?>
        <h1>Upload a New Image</h1>
        <p>
            Please fill out the following form and press the <i>Upload</i> button.
        </p>
        <div class="row">
            <div class="col-md-6">
                <?= $this->form()->openTag($form); ?>
                <div class="form-group">
                    <?= $this->formLabel($form->get('name')); ?>
                    <?= $this->formElement($form->get('name')); ?>
                    <?= $this->formElementErrors($form->get('name')); ?>
                </div>
                <div class="form-group">
                    <?= $this->formLabel($form->get('avatar')); ?>
                    <?= $this->formElement($form->get('avatar')); ?>
                    <?= $this->formElementErrors($form->get('avatar')); ?>
                </div>        
                <?= $this->formElement($form->get('submit')); ?>
                <?= $this->form()->closeTag(); ?>
            </div>    
        </div>
        <div class="row">
        <table class="table table-hover">
          <thead>
            <tr>
              <th scope="col">Type</th>
              <th scope="col">Image Id</th>
              <th scope="col">Image Name</th>
              <th scope="col">Image Avatar</th>
            </tr>
          </thead>
          <tbody>
              <?php foreach($pic as $pic):?>
         <tr>
          <th> <h3><?= $this->escapeHtml($pic->getId()); ?>  </h3></th>
          <td></td>
          <td></td>
          <td></td>
        </tr>
             <?php endforeach; ?>
          </tbody>
        </table>
        </div>

我遇到此错误

  

注意:未定义的变量:第41行中的C:\ xampp \ htdocs \ form \ module \ Crud \ view \ crud \ crud \ upload.phtml中的图片

     

警告:第41行的C:\ xampp \ htdocs \ form \ module \ Crud \ view \ crud \ crud \ upload.phtml中为foreach()提供的参数无效

如何在zend框架中定义变量?
 如何清除未定义变量的zend框架中的错误? 我试图解决zend框架中的问题,但仍未解决 显示未定义的内容是什么? 我试图用谷歌搜索错误但没有得到答案 我在控制器和经理中也定义了变量 如果需要更多代码,我会帮助 我试图将变量从控制器传递给视图,但没有结果 预先感谢

2 个答案:

答案 0 :(得分:0)

尝试在$this->pic文件中使用.phtml代替$ pic。

<?php foreach($this->pic as $pic):?>

如果您是根据渲染文件的相对动作在“ pic”键中设置数据,则此方法有效。

答案 1 :(得分:0)

请尝试以下代码:

             <?php if (isset($this->pic) && ! empty($this->pic)) : ?>

                  <?php foreach($this->$pic as $pic):?>
                     <tr>
                      <th> <h3><?= $this->escapeHtml($pic->getId()); ?>  </h3></th>
                      <td></td>
                      <td></td>
                      <td></td>
                    </tr>
                 <?php endforeach; ?>

             <?php endif; ?>