dongpu3347 2014-04-15 14:39
浏览 68
已采纳

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

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

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

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

}

and finally here is the structure of the root :

module 

-----\Application

------\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
  • 写回答

1条回答 默认 最新

  • doushi1900 2014-04-15 14:56
    关注

    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()
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题