致命错误:Class' Application \ forms \ ArticleForm'在我的控制器中找不到

时间:2014-02-25 15:58:15

标签: php zend-framework zend-framework2 zend-form zend-studio

所以我有一个让我疯狂的问题:(我创建了一个表单,当我在控制器中使用类表单时出现了这个错误:

Fatal error: Class 'Application\forms\ArticleForm' not found in C:\wamp2\www\test\module\Application\src\Application\Controller\BlogController.php

即使我尝试use Application\forms\ArticleForm找不到此路径,这也是我的行动的一部分:更新代码:

public function addAction()
{       
    $form = new ArticleForm();// here the class doesn't found !!
    //var_dump($form);die;
    $form->initForm();
    $request = $this->getRequest();
    $form->setData($request->getPost());

这是我的ArticleForm:

class ArticleForm extends Form {

 public function __construct()
 {
    parent::__construct('UserEntry');

$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
$this->setAttribute('class', 'contact_form');
 }

public function initForm()
  {
  $this->addFormFields(); //function where we added all fields

$articleInputFilter = new ArticleInputFilter();
$this->setInputFilter($articleInputFilter->getInputFilter()); //Asign input Filter to      form
}

protected function addFormFields()
 {
   $this->addSubmit();
   $this->addTitle();
   $this->addContent();
   $this->addDate();
   $this->addPublication();
   $this->addImage();
  }

/**
 *
 */
protected function addTitle()
{
    $this->add(array(
            'name' => 'title',
            'attributes' => array(
                    'type' => 'text',
            ),
            'options' => array(
                    'label' => _('Title')
            ),
    ));
}

/**
 *
 */
protected function addContent()
{
    $this->add(array(
            'name' => 'content',
            'attributes' => array(
                    'type' => 'text',
            ),
            'options' => array(
                    'label' => _('Content')
            ),
    ));
}

/**
 *
 */
protected function addDate()
{
    $this->add(array(
            'name' => 'date',
            'attributes' => array(
                    'type' => 'date',
            ),
            'options' => array(
                    'label' => _('Date'),
                    'id'    => 'datepicker',
            ),
    ));
}

/**
 *
 */
protected function addPublication()
{
    $this->add(array(
            'name' => 'publication',
            'attributes' => array(
                    'type' => 'checkbox',
            ),
            'options' => array(
                    'label' => _('Publication'),
                    'use_hidden_element' => true,
                    'checked_value' => 1,
                    'unchecked_value' => 'no',
            ),
    ));
}

/**
 *
 */
protected function addImage()
{
    $this->add(array(
            'name' => 'Image',
            'attributes' => array(
                    'type' => new ImageForm(),
            ),
            'options' => array(
                    'label' => _('Image')
            ),
    ));
}

/**
 *
 */
protected function addSubmit()
{
    $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                    'type' => 'submit',
                    'value' => _('Add'),
                    'class' => 'submit',
            ),
    ));
}
}

最后这是我的ArticleInputFilter:

 class ArticleInputFilter extends InputFilter implements InputFilterAwareInterface 
  {
/**
 * @var string
 */
public $title;

/**
 * @var int
 */
public $image;

/**
 * @var string
 */
public $content;

/**
 * @var Date
 */
public $date;

/**
 * @var Boolean
 */
public $publication;

/**
 * @param $data
 */
public function exchangeArray($data)
{
    $this->title     = (isset($data['title']))     ? $data['title']     : $this->title;
    $this->image     = (isset($data['image']))     ? $data['image']     :  $this->image;
    $this->content     = (isset($data['content']))     ? $data['content']     : $this->content;
    $this->publication   = (isset($data['publication']))       ? $data['publication']   :  $this->publication;
    $this->date      = (isset($data['date']))      ? $data['date']  :  $this->date;
}

/**
 * @param InputFilterInterface $inputFilter
 * @return void|InputFilterAwareInterface
 * @throws \Exception
 */
public function setInputFilter(InputFilterInterface $inputFilter)
{
    throw new \Exception("Not used");
}

/**
 * @return InputFilter|InputFilterInterface
 */
public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();

        $inputFilter->add($factory->createInput(array(
                'name'     => 'title',
                'required' => true,
                'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                ),
                'validators' => array(
                        array(
                                'name'    => 'StringLength',
                                'options' => array(
                                        'encoding' => 'UTF-8',
                                        'min'      => 6,
                                        'max'      => 100,
                                ),
                        ),
                ),
        )));

        $inputFilter->add($factory->createInput(array(
                'name'     => 'content',
                'required' => true,
                'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                ),
                'validators' => array(
                        array(
                                'name'    => 'StringLength',
                                'options' => array(
                                        'encoding' => 'UTF-8',
                                        'min'      => 10,
                                ),
                        ),
                ),
        )));

        $inputFilter->add($factory->createInput(array(
                'name'     => 'publication',
                'required' => false,
        )));

        $inputFilter->add($factory->createInput(array(
                'name'     => 'date',
                'required' => true,
        )));

        $inputFilter->add($factory->createInput(array(
                'name'     => 'image',
                'required' => true,
        )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}
  }

所以,如果有人对我的问题有任何想法或解决方案,我将非常感激。

2 个答案:

答案 0 :(得分:2)

可能是自动加载问题。

在哪里定义了您的ArticleForm

注意:您最好使用表单元素管理器来获取表单实例。您可以阅读有关此主题的更多信息here

答案 1 :(得分:0)

我遇到了同样的问题。答案很简单。问题出在自动加载器中,因此您需要在开始之前“刷新”或“sincronyze everything”。因此,在函数addAction()的顶部或文件只需要供应商文件夹内的autoload.php文件来“更新”您的项目。像这样:

require 'vendor/autoload.php';

显然,你需要编写自己的路径。 我希望这会有所帮助。