如何更改默认模块?

How to change default module?

我已经安装了ZF3,默认模块是"Application"。我想通过其他方式默认更改此模块。我该怎么做?

编辑我: 我做了一些更改,但没有用:

/config/modules.config.php

return [
    'Zend\Router',
    'Zend\Validator',
    'Test',
];

/module/Test/config/module.config.php

<?php
namespace Test;

use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'router' => [
        'routes' => [
            'home' => [
                'type'    => "Literal",
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/',
                    'defaults' => [
                        'controller'    => Controller\IndexController::class,
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    // You can place additional routes that match under the
                    // route defined above here.
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'Test' => __DIR__ . '/../view',
        ],
    ],
];

当我尝试访问 http://localhost 时得到的结果是:

Fatal error: Uncaught Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "error"; resolver could not resolve to a file in C:\Apache24\htdocs\shop\vendor\zendframework\zend-view\src\Renderer\PhpRenderer.php:494 Stack trace: #0 C:\Apache24\htdocs\shop\vendor\zendframework\zend-view\src\View.php(207): Zend\View\Renderer\PhpRenderer->render() #1 C:\Apache24\htdocs\shop\vendor\zendframework\zend-view\src\View.php(236): Zend\View\View->render(Object(Zend\View\Model\ViewModel)) #2 C:\Apache24\htdocs\shop\vendor\zendframework\zend-view\src\View.php(200): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel)) #3 C:\Apache24\htdocs\shop\vendor\zendframework\zend-mvc\src\View\Http\DefaultRenderingStrategy.php(105): Zend\View\View->render(Object(Zend\View\Model\ViewModel)) #4 C:\Apache24\htdocs\shop\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent)) 5 C:\Apache24\htdocs\shop\ve in C:\Apache24\htdocs\shop\vendor\zendframework\zend-view\src\Renderer\PhpRenderer.php on line 494

编辑二(固定):

/config/modules.config.php

return [
    'Zend\Router',
    'Zend\Validator',
    'Test',
];

/module/Test/config/module.config.php

<?php
namespace Test;

use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'router' => [
        'routes' => [
            'home' => [
                'type'    => "Literal",
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/',
                    'defaults' => [
                        'controller'    => Controller\IndexController::class,
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    // You can place additional routes that match under the
                    // route defined above here.
                ],
            ],
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'test/index/index' => __DIR__ . '/../view/test/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],        
        'template_path_stack' => [
            'Test' => __DIR__ . '/../view',
        ],
    ],
];

最后,我将 "error" 和 "layout" 目录添加到我的模块 "Test"。

您必须执行以下操作:

  • /config/modules.config.php 中,将 Application 替换为您的模块名称。不要忘记将 Zend\Router 保留在要加载的模块列表中,在您的模块之前。
  • 在您的模块中,编写一个 config/module.config.php 文件,如 /module/Application/config/module.config.php 并将 application 替换为您的模块名称(尤其是在 template_map 数组中)。
  • /composer.json 部分 autoloadautoload-dev 中,将对 "application" 的引用替换为您的模块名称。然后 运行 composer update 更新 composer/autoload_... 个文件。
  • view/error 目录添加到包含 index.phtml404.phtml 文件的模块的文件夹中。
  • 在模块的文件夹中添加一个 view\layout\layout.phtml 文件。

小心命名空间! 这应该有效。