简体   繁体   English

致命错误:在Zend Framework 2中找不到类

[英]Fatal error : class not found in Zend framework 2

I'am stuck in zend framework , i am new to it and trying to implement a website just to learn it . 我被zend框架所困扰,我是一个新手,试图建立一个网站只是为了学习它。 So my website is about pizzas . 所以我的网站是关于披萨的。 when I try to add a pizza , after sending form data , i get this error message saying : Fatal error: Class 'PizzaPoint\\Controller\\Pizza' not found in C:\\wamp\\www\\pizzalast\\module\\PizzaPoint\\src\\PizzaPoint\\Controller\\PizzaController.php on line 27 , at this line exactly i instantiate an object of the class Pizza which is located in the " Model " folder . 当我尝试添加比萨饼时,发送表单数据后,出现以下错误消息:致命错误:在C:\\ wamp \\ www \\ pizzalast \\ module \\ PizzaPoint \\ src \\ PizzaPoint中找不到类'PizzaPoint \\ Controller \\ Pizza' \\ Controller \\ PizzaController.php在第27行,在这一行,我实例化了位于“模型”文件夹中的Pizza类的对象。

this the add action from the pizza controller : 这是披萨控制器的添加操作:

public function addAction()
    {
    $form = new PizzaForm();
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();
    if($request->isPost())
        {
        $pizza = new Pizza();
        $form->setInputFilter($pizza->getInputFilter());
        $form->setData($request->getPost());

            if($form->isValid())
                {
                    $pizza->exchangeArray($form->getData());
                    $this->getPizzaTable()->savePizza($pizza);
                }       
        }   
    return array('form' => $form);
    }

these are the first 40 lines of code of the Pizza.php file : 这是Pizza.php文件的前40行代码:

namespace PizzaPoint\Model;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;

use Zend\InputFilter\InputFilterInterface;

class Pizza implements InputFilterAwareInterface {

public $id;
public $title;
public $zutaten;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;

protected $inputFilter;

public function exchangeArray($data)
    {
        $this->id            = (!empty($data['id']))         ? $data['id']         : null;
        $this->title         = (!empty($data['title']))      ? $data['title']      : null;
        $this->zutaten       = (!empty($data['zutaten']))    ? $data['zutaten']    : null;
        $this->smallprice    = (!empty($data['smallprice'])) ? $data['smallprice'] : null;
        $this->bigprice      = (!empty($data['bigprice']))   ? $data['bigprice']   : null;
        $this->familyprice   = (!empty($data['familyprice']))? $data['familyprice']: null;
        $this->partyprice    = (!empty($data['partyprice'])) ? $data['partyprice'] : null;
    }

public function getArrayCopy()
    {
        return get_object_vars($this);
    }
public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

the third thing i think i should afford here is the module.php file 我认为我应该在这里负担的第三件事是module.php文件

namespace PizzaPoint;

use PizzaPoint\Model\Pizza;

use PizzaPoint\Model\PizzaTable;

use Zend\Db\ResultSet\ResultSet;

use Zend\Db\TableGateway\TableGateway;

class Module {

public function getAutoloaderConfig()
    {
        return array('Zend\Loader\ClassMapAutoloader'=>array(__DIR__ . '/autoload_classmap.php',),
        'Zend\Loader\StandardAutoloader'=>array('namespaces'=>array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__ , ),),);
    }
public function getConfig()
    {
    return include __DIR__ . '/config/module.config.php';   
    }

public function getServiceConfig()
    {
        return array(
          'factories' => array(
              'PizzaPoint\Model\PizzaTable' => function($sm) 
                    {
                        $tableGateway = $sm->get('PizzaTableGateway');
                        $table = new PizzaTable($tableGateway);
                        return $table;
                    },
               'PizzaTableGateway' => function ($sm)
                    {
                       $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                       $resultSetPrototype = new ResultSet();
                       $resultSetPrototype->setArrayObjectPrototype(new Pizza());
                       return new TableGateway('pizza', $dbAdapter, null, $resultSetPrototype); 
                    },
              ),
            );
    }

} }

and finally here is the structure of the root : 最后是根的结构:

module 

-----\\Application - - -\\应用

------\\PizzaPoint ------ \\ PizzaPoint

    2. -----\config



              3 ------\ module.config.php

    2------\src
                   3 ------\PizzaPoint
                           4 --------\Controller
                                    5 -------\PizzaController.php
                           4---------\Form
                                     5--------\PizzaForm.php
                           4 ---------\Model
                                    5--------\Pizza.php
                                    5--------\PizzaTable.php

Since your controller is within the PizzaPoint\\Controller namespace, when you run new Pizza() , PHP thinks you mean new PizzaPoint\\Controller\\Pizza() . 由于您的控制器位于PizzaPoint\\Controller命名空间内,因此当您运行new Pizza() ,PHP认为您的意思是new PizzaPoint\\Controller\\Pizza() You either want to use the global namespace: 您要么要使用全局名称空间:

new \PizzaPoint\Model\Pizza()

or (even better), add: 或(甚至更好),添加:

use PizzaPoint\Model\Pizza;

to the top of the controller class (below the namespace declaration) to import that class into the current namespace. 到控制器类的顶部(在名称空间声明下方),以将该类导入当前名称空间。 Then your existing code should work. 然后,您现有的代码应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM