在 Zend Framework 2 中找不到创建新动作控制器的页面

Page not found creating a new action controller in Zend Framework 2

我遇到了一个大问题,因为我可以看到 index.html 由 IndexController 提供,但是如果我创建另一个操作,例如 getdataAction(),我会遇到下一个错误:

我想我不需要为此配置任何东西,但这些是我的文件:

module_config.php

namespace Application;

return array(
'doctrine' => array(
        'driver' => array(
                'application_entities' => array(
                        'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                        'cache' => 'array',
                        'paths' => array(__DIR__ . '/../src/Application/Entity')
                ),

                'orm_default' => array(
                        'drivers' => array(
                                'Application\Entity' => 'application_entities'
                        )
                )
        )
),      
'router' => array(
    'routes' => array(
          'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index'
                ),
            ),
        ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/Application',
                'defaults' => array(
                    __NAMESPACE__ => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => Controller\IndexController::class
        //'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/layout.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' => array(
        __DIR__ . '/../view',
    ),
),
// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),
);

IndexController.php

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\UserEntity;

class IndexController extends AbstractActionController
{
protected $_objectManager;

public function indexAction(){
    try{    
        $users = $this->getObjectManager()->getRepository('\Application\Entity\UserEntity')->findAll();

        foreach ($users as $user){
            echo "[id]: " . $user->getId() . " Nombre: " . $user->getFullName() . "<br />";
        }
    }catch(Exception $ex){
        echo "error: " . $ex->getMessage();
    }   
    return new ViewModel(array("users"  =>  $users));
}

public function getdataAction(){
    return new ViewModel();
}

protected function getObjectManager()
{
    if (!$this->_objectManager) {
        $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    }

    return $this->_objectManager;
}    
}

目录结构如下:

您没有任何行动途径。

框架应用程序定义了一个 catch all 路由作为标准,您可以通过以下方式调用它:

/application/:controller/:action

所以你应该可以使用:

/application/index/getdata

要获得更好的路线,您需要在 module_config 中定义一条路线。

 'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/Application',
                'defaults' => array(
                    __NAMESPACE__ => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),

此代码错误,您必须引用此 NAMESPACE

 'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/Application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),

它将解决这个问题

Index(resolves to invalid controller class or alias: Index)

另一方面,你使用这个:

'invokables' => array(
        'Application\Controller\Index' => Controller\IndexController::class

你几乎有这个权利,但你必须输入完整的完全合格的 Class 名称 (FQCN)

'invokables' => array(
        'Application\Controller\Index' => Application\Controller\IndexController::class

编辑:

您将主路由定义为 /Application

'defaults' => array(
                    __NAMESPACE__ => 'Application\Controller',
                    'controller'    => 'Index',

ucfirst这里有事试试。

http://localhost/Application/Index/getdata

http://localhost/application/index/getdata