PHP Zend 2:致命错误 - 找不到类

时间:2016-09-06 07:21:32

标签: php zend-framework zend-framework2

我正在尝试创建自己的模型来执行某些操作(将进行计算,移动数据库以及其他未指定的工作)。但是我遇到了一个问题,因为我仍然得到并且没有找到Class的错误。

这是控制器(module / Application / src / Application / Controller / IndexController.php):

namespace Application\Controller;

use Application\Model\Przesylki;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{    

    public function indexAction()
    {
        $this->layout('layout/layout');
    }

    public function cennikAction() {
        $this->layout('layout/pusty');
        $logika_paczki = new Przesylki();
        echo $logika_paczki->return_word();
    }
}

Module.php(module / Application / src / Module.php):

namespace Application;

use Main\Model\Przesylki;

class Module
{
    const VERSION = '3.0.2dev';

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

和模型,Przesylki.php(module / Application / src / Application / Model / Przesylki.php):

namespace Main\Model;

class Przesylki
{
    public function return_word() {
        return "word";
    }
}

如有必要,autoload_classmap.php(module / Application / src / autoload_classmap.php只是一个

return array();

module.config.php:

namespace Application;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            // Home
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'counter',
                    ],
                ],
            ],
            'work' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/work',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'cennik' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/cennik',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'cennik',
                    ],
                ],
            ],
            // Panel
            'panel' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/panel',
                    'defaults' => [
                        'controller' => Controller\PanelController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'test' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/panel/test',
                    'defaults' => [
                        'controller' => Controller\PanelController::class,
                        'action'     => 'test',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\PanelController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'layout/panel'           => __DIR__ . '/../view/layout/panel_layout.phtml',
            'layout/counter'           => __DIR__ . '/../view/layout/counter.phtml',
            'layout/pusty'           => __DIR__ . '/../view/layout/pusty.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

目录:

/config
    /autoload
/data
    /cache
/module
    /Application
        /config
            module.config.php
        /src
            /Application
                /Controller
                    IndexController.php
                    PanelController.php
                /Model
                    Przesylki.php
            autoload_classmap.php
            Module.php
        /test
        /view
/public

当我跑/ cennik我得到一个致命的时候,找不到那个班级:

[Tue Sep 06 08:58:20.446371 2016] [:error] [pid 4934] [client 195.8.99.234:1129] PHP Fatal error:  Class 'Main\\Model\\Przesylki' not found in /var/www/Paczucha_pl/module/Application/src/Main/IndexController.php on line 27

实际文件和命名空间:

Przesylki.php - Application\Model
IndexController - Application\Controller
PanelController - Application\Controller
module.config.php - Application
Module.php - Application

2 个答案:

答案 0 :(得分:2)

您的命名空间存在问题。您的模块名称为Application,因此您的模型名称空间应为Application\Main\Model

您的模块结构不是ZF2推荐的模块结构。它应该是:

/module
     /Application
         /config
             module.config.ph
         /src
             /Application
                 /Controller
                     IndexController.php   // => Namespace : Application\Controller
                     PanelController.php   // => Namespace : Application\Controller
                 /Model
                     Przesylki.php         // => Namespace : Application\Model
         /view
             /index
                 /index
             /panel
                 /panel

答案 1 :(得分:2)

好的,我终于找到了解决方案。给定命名空间的正确结构是:

/module
     /Application
         /config
             module.config.php
         /src
             /Controller
                 IndexController.php   // => Namespace : Application\Controller
                 PanelController.php   // => Namespace : Application\Controller
             /Model
                 Przesylki.php         // => Namespace : Application\Model
         /view
         /index
             /index
         /panel
             /panel

不应该有双重Application文件夹。我认为它是Zend 3,也许它已经改变了。