zend framwork2类不存在

时间:2015-12-25 16:31:51

标签: php zend-framework zend-framework2

我创建了控制器和视图,但是我收到了这个错误:

Zend \ Mvc \ Controller \ ControllerManager :: createFromInvokable:通过可调用类“Newproject \ Controller \ new_controller”检索“newprojectcontrollernewproject(别名:Newproject \ Controller \ Newproject)”失败;类不存在

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

控制器类new_controller.php

namespace Newproject\Controller;

use Zend\Mvc\Controller\AbstractActionController; 

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

1 个答案:

答案 0 :(得分:0)

http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html中所述,控制器必须以大写字母开头。遵循标准命名约定也是一种好习惯,这意味着使用NewController代替非常非常旧学校下划线类命名约定(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()
    {
    } 
}