0
votes

I have installed ZF3 and the module by default is "Application". I would like to change this module by default by other. How can I do that?

Edit I: I have made some changes but it doesn't work:

/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',
        ],
    ],
];

And the result that I get when I try to access to http://localhost is:

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

Edit II (fixed):

/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',
        ],
    ],
];

And finally, I have added "error" and "layout" directory to my module "Test".

enter image description here

1

1 Answers

2
votes

You must do the following:

  • In /config/modules.config.php, replace Application with the name of your module. Do not forget to keep Zend\Router in the list of modules to be loaded, before your modules.
  • In your module, write a config/module.config.php file like /module/Application/config/module.config.php and replacing application by the name of your module (especially in the template_map array).
  • In /composer.json, section autoload and autoload-dev, replace the references to "application" with the name of your module. Then run composer update to update composer/autoload_... files.
  • Add a view/error directory to the folder of your module with theindex.phtml and 404.phtml files.
  • Add a view\layout\layout.phtml file in the folder of your module.

Beware of namespace ! This should work.