zend framwork2 class 不存在

zend framwork2 class does not exist

我创建了控制器和视图,但出现此错误:

Zend\Mvc\Controller\ControllerManager::createFromInvokable:通过可调用 class "Newproject\Controller\new_controller" 检索 "newprojectcontrollernewproject(alias: Newproject\Controller\Newproject)" 失败; class不存在

Module.php

namespace Newproject;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

module.config.php

return array(
    'controllers' => array(
        'invokables' => array('Newproject\Controller\Newproject' => 'Newproject\Controller\new_controller'),
    ),
    'router' => array(
        'routes' => array(
            'newproject' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/newproject[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+'),
                        'defaults' => array(
                            'controller' => 'Newproject\Controller\Newproject',
                            'action' => 'index'
                        ),
                    )
                )
            )
        ),
        'view_manager' => array('template_path_stack' => array('newproject' => __DIR__.'/../view'),
    ),
);

控制器classnew_controller.php

namespace Newproject\Controller;

use Zend\Mvc\Controller\AbstractActionController; 

class new_controller extends AbstractActionController 
{
    public function indexAction()
    {
    } 
}

http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html 中所述,控制器必须以大写字母开头。坚持标准命名约定也是一种很好的做法,这意味着使用 NewController 而不是非常、非常 老式的下划线 class 命名约定(new_controller).

如果这不起作用,请查看 config/application.config.php。如果启用了配置文件缓存,则需要先删除相关文件才能生效。

根据您发布的内容,我建议进行以下更改:

<?php

return array(
    'controllers' => array(
        'invokables' => array(
            'NewProject\Controller\New' => 'NewProject\Controller\NewController'
        ),
    ),
    'router' => array(
        'routes' => array(
            'new_project' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/new-project[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'NewProject\Controller',
                        'controller' => 'New',
                        'action' => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'new-project' => __DIR__.'/../view'
        ),
    ),
);

和控制器...

<?php

namespace NewProject\Controller;

use Zend\Mvc\Controller\AbstractActionController; 

class NewController extends AbstractActionController 
{
    public function indexAction()
    {
    } 
}