Zend Framework 2 MVC - Modules Route mapping not working

让人想犯罪 __ 提交于 2019-12-19 04:15:17

问题


I try to follow Akrabats Tutorial the Application/Index is working, the Album part not.

I tried it also with the ZendSkeletonModule with no luck.

The error in both cases is:

album/album (resolves to invalid controller class or alias: album/album)

I tried it with ZF2 master and beta4 tag (but beta4 tag gives php error about missing method getEventManager)

I took the code from Akrabats Tutorial, and after that failed used the code form the GitHub Repo. Unfortunatly there isnt some Forum or Comment section to ask for help.

I found some diffrences in the tutorial and the Skeleton (zfcUser has the same diffrence) in the module.config.php (which i believe is the core of the problem).

The tutorial uses classes in the controller index, zfcUser and the Skeleton using invokables but it doesnt seem to matter, as the error dont change.

my module.config currently looks like this:

<?php

return array(

    // Controllers in this module
    'controller' => array(
        'invokables' => array(
            'album/album' => 'Album\Controller\AlbumController',
        ),        
    ),


    // Routes for this module
    'router' => array(
        'routes' => array(
            'album' => array(
                'type' => 'Literal',
                'priority' => 1000,
                'options' => array(
                    'route' => '/album',
                    'defaults' => array(
                        'controller' => 'album/album',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array( 
                    'misc' => array (
                        'type'    => 'segment',
                        'options' => array(
                            'route'    => '/album/[:action][/:id]',
                            'constraints' => array(
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id'     => '[0-9]+',
                            ),
                            'defaults' => array(
                                'controller' => 'album/album',
                                'action'     => 'index',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),    

    // View setup for this module
    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

Album\Controller\AlbumController:

<?php

namespace Album\Controller;

use Zend\Mvc\Controller\ActionController,
    Zend\View\Model\ViewModel,
    Album\Model\AlbumTable,
    Album\Model\Album,
    Album\Form\AlbumForm;

class AlbumController extends ActionController
{
// [....]
}

I dont know where to look to fix this error, does someone of you have an idea?

Code is like orginal on github (see links above) when not mentioned otherwise.

TIA


回答1:


The difference here is not only the invokables, but also that the array key is now "controllers" in plural, wich did the trick for me. In your code "invokables" is already changed, but you seem to use "controller" as key.

    'controllers' => array(
            'invokables' => array(
                    'album/album' => 'Album\Controller\AlbumController',
            ),
    ),



回答2:


Apparently there is a change in the array for the controller. Now the classes key is changed to invokables: At the module.config.php

    'controller' => array(
            'classes' => array(
                    'album/album' => 'Album\Controller\AlbumController',
            ),
    ),

has to be changed to

    'controllers' => array(
            'invokables' => array(
                    'album/album' => 'Album\Controller\AlbumController',
            ),
    ),



回答3:


Found it, it The ActionController class seems to be gone on ZF2-beta4 so your AlbumController needs to be this way:

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController {
   .....
}



回答4:


You must right config/application.config.php

<?php
return array(
    'modules' => array(
        'Application',
        'Album',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);


来源:https://stackoverflow.com/questions/11340932/zend-framework-2-mvc-modules-route-mapping-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!