致命错误:在Zend框架中找不到类2 [英] Fatal error : class not found in Zend framework 2

查看:132
本文介绍了致命错误:在Zend框架中找不到类2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在zend框架中,我是新的,试图实现一个网站只是为了学习它。所以我的网站是关于比萨饼。当我尝试添加比萨饼,发送表单数据后,我得到这个错误消息说:致命错误:类'PizzaPoint\Controller\Pizza'没有找到C:\wamp\www\pizzalast\module \PizzaPoint\src\PizzaPoint\Controller\PizzaController.php在第27行,在这一行正好实例化一个对象类Pizza,它位于模型文件夹中。

I'am stuck in zend framework , i am new to it and trying to implement a website just to learn it . 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 .

这是来自披萨控制器的添加操作:

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

这些是Pizza.php文件的前40行代码: / p>

these are the first 40 lines of code of the Pizza.php file :

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

我认为我应该负担的第三件事是module.php文件

the third thing i think i should afford here is the module.php file

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

}

最后这里是根的结构:

module 

----- \Application

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


推荐答案

由于您的控制器位于 PizzaPoint\Controller 命名空间中,当您运行 new Pizza()时,PHP认为您的意思是 new PizzaPoint\Controller\Pizza()。您要使用全局命名空间:

Since your controller is within the PizzaPoint\Controller namespace, when you run new Pizza(), PHP thinks you mean new PizzaPoint\Controller\Pizza(). You either want to use the global namespace:

new \PizzaPoint\Model\Pizza()

或更好地添加:

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.

这篇关于致命错误:在Zend框架中找不到类2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆