Zend Framework 2 MVC - Modules Route mapping not w

2019-02-15 17:36发布

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

4条回答
Juvenile、少年°
2楼-- · 2019-02-15 18:08

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',
        ),
    ),
);
查看更多
甜甜的少女心
3楼-- · 2019-02-15 18:19

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',
            ),
    ),
查看更多
\"骚年 ilove
4楼-- · 2019-02-15 18:20

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',
            ),
    ),
查看更多
爷、活的狠高调
5楼-- · 2019-02-15 18:23

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 {
   .....
}
查看更多
登录 后发表回答