同一模块中的ZF3多个控制器

时间:2018-03-01 18:23:22

标签: zend-framework3

我正在学习ZF3,我正在尝试将InfoController添加到Album模块。我的网址会是...... / album / info吗?我发生了404错误。我见过John Deck's post并实现完全一样,但仍无法正常工作

2 个答案:

答案 0 :(得分:0)

这是我的module.config.php

<?php
namespace Album;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return array(
    'controllers' => [
        'factories' => [
            Controller\InfoController::class => function($container) {
                return new Controller\InfoController(
                    $container->get(\Album\Model\InfoTable::class)
                );
            },
            Controller\AlbumController::class => function($container) {
                return new Controller\AlbumController(
                    $container->get(\Album\Model\AlbumTable::class)
                );
            },
        ],
    'aliases' => [
        'index' =>  AlbumController::class,
        'info'  =>  InfoController::class,
        ]
    ],

    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => Segment::class,
                'options' => array(
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\AlbumController',//Controller::class,
                        'action'     => 'index',
                    ),
                ),      
            ),
            'info' => array(
                'type'    => Segment::class,
                'options' => array(
                    //'route' => 'Album/Controller/Info[/:action[/:id]]',
                    'route' => '/InfoController[/:action[/:id]]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Info',//::class
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),


    'view_manager' => [
    'display_not_found_reason' => false,
    'display_exceptions'       => false,
    'doctype'              => 'HTML5',
    'template_map'  =>  [
        'layout/album'    => __DIR__ . '/../view/layout/album_layout.phtml',
            'album/album/index'    => __DIR__ . '/../view/album/album/index.phtml',
        'layout/album'    => __DIR__ . '/../view/layout/info_layout.phtml',
            'info/info/index'    => __DIR__ . '/../view/info/info/index.phtml',
        ],
        'template_path_stack' => [
            'Album' => __DIR__ . '/../view',
            'Info' => __DIR__ . '/../view',
        ],
    ],

);

答案 1 :(得分:0)

Here's my Module.php

<?php

namespace Album;

use Album\Model\InfoTable;
use Album\Model\Info;

use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{

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

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                Model\AlbumTable::class => function($container) {
                    $tableGateway = $container->get(Model\AlbumTableGateway::class);
                    return new Model\AlbumTable($tableGateway);
                },
                Model\AlbumTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
                Model\InfoTable::class => function($container) {
                    $tableGateway = $container->get(Model\InfoTableGateway::class);
                    return new Model\InfoTable($tableGateway);
                },
                Model\InfoTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Info());
                    return new TableGateway('info', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }

    public function getControllerConfig()
    {
        return array(
            'factories' => array(
                Controller\AlbumController::class => function($container) {
                    return new Controller\AlbumController(
                        $container->get(Model\AlbumTable::class)
                    );
                },
                Controller\InfoController::class => function($container) {
                    return new Controller\InfoController(
                        $container->get(Model\InfoTable::class)
                    );
                },

            ),
        );
    }
}