首页 文章

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

提问于
浏览
1

这是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警告:在C:\ xampp \ htdocs \中为foreach()提供的参数无效第41行的form \ module \ Crud \ view \ crud \ crud \ upload.phtml

如何在zend框架中定义变量?
如何删除未定义变量的zend框架中的错误?我试图解决zend框架中的问题,但仍未解决显示未定义的问题?我试图谷歌错误但没有得到答案我已经定义变量控制器以及经理也如果需要更多的代码我将帮助我试图从控制器传递变量查看但没有结果在此先感谢

1 回答

  • 0

    尝试在 .phtml 文件中使用 $this->pic 而不是$ pic .

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

    如果您使用渲染文件的相对操作在“pic”键中设置数据,这将起作用 .

相关问题