gpt4 book ai didi

php - fatal error : class not found in Zend framework 2

转载 作者:行者123 更新时间:2023-12-02 17:38:59 25 4
gpt4 key购买 nike

我被困在 zend 框架中,我是新手,并试图实现一个网站只是为了学习它。所以我的网站是关于比萨饼的。当我尝试添加披萨时,发送表单数据后,我收到此错误消息: fatal error :在 C:\wamp\www\pizzalast\module\PizzaPoint\src\PizzaPoint 中找不到类 'PizzaPoint\Controller\Pizza'\Controller\PizzaController.php 在第 27 行,正是在这一行,我实例化了位于“Model”文件夹中的 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 行代码:

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 文件

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 

-----\应用

------\比萨点

    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

最佳答案

由于您的 Controller 位于 PizzaPoint\Controller 命名空间内,因此当您运行 new Pizza() 时,PHP 认为您的意思是 new PizzaPoint\Controller\Pizza( )。您要么想要使用全局命名空间:

new \PizzaPoint\Model\Pizza()

或者(甚至更好)添加:

use PizzaPoint\Model\Pizza;

到 Controller 类的顶部(命名空间声明下方)以将该类导入当前命名空间。那么您现有的代码应该可以工作。

关于php - fatal error : class not found in Zend framework 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com