简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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