不懂模块

I don't understand Modules

我正在尝试学习 Zend Framework 3,我已经完成了相册教程和另一个博客教程。我一直在直接编写 php 应用程序大约 3 年,并使用 Slim 3 编写了 2 个 Web 应用程序。我正在尝试围绕模块进行思考。我不放弃理解模块是什么。我理解模块的方式是,模块是主应用程序内部的一个小应用程序。我试图在同一个模块中创建 2 个控制器并从这些控制器呈现不同的视图。我搜索了一整天,找不到创建两个控制器并让它们路由到不同视图的方法。但是,我发现了一个应用程序的许多示例,该应用程序具有 5 个不同的模块,所有模块都只有一个控制器,并且 class 方法可能指向不同的视图。我的理解是应该为每个页面创建一个模块吗?例如,一个登录模块和一个关于模块和一个联系模块?

module.config.php

    <?php

namespace Blog;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;


return [
    // this line opens the configuration fro the Route Manager
    'router' => [
        // open configuration for all possible routes
        'routes' => [
            // Define a new route called 'blog'
            'blog' => [
                // define 'literal' route type:
                'type' => Literal::class,
                // configure the route itself
                'options' => [
                    // listen to '/blog' as uri:
                    'route' => '/blog',
                    // define default controller and action to be called when
                    // this route is matched
                    'defaults' => [
                        'controller' => Controller\ListController::class,
                        'action' => 'index',
                    ],
                    'login' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/blog/login',
                            'defaults' => [
                                'controller' => Controller\LoginController::class,
                                'action' => 'login',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
    'service_manager' => [
        'aliases' => [
            Model\PostRepositoryInterface::class => Model\PostRepository::class,
        ],
        'factories' => [
            Model\PostRepository::class => InvokableFactory::class,
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\ListController::class => Factory\ListControllerFactory::class,
            Controller\LoginController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_map' => [
            'login/login' => __DIR__ . '/../view/blog/login/login.phtml'
        ],
        'template_path_stack' => [
            __DIR__ . '/../view'
        ],
    ]
]; 

文件结构

 |-- module
        |   |-- Application
        |   |   |-- config
        |   |   |   |-- module.config.php
        |   |   |-- src
        |   |   |   |-- Module.php
        |   |   |   |-- Controller
        |   |   |       |-- IndexController.php
        |   |   |-- test
        |   |   |   |-- Controller
        |   |   |       |-- IndexControllerTest.php
        |   |   |-- view
        |   |       |-- application
        |   |       |   |-- index
        |   |       |       |-- index.phtml
        |   |       |-- error
        |   |       |   |-- 404.phtml
        |   |       |   |-- index.phtml
        |   |       |-- layout
        |   |           |-- layout.phtml
        |   |-- Blog
        |       |-- config
        |       |   |-- module.config.php
        |       |-- src
        |       |   |-- Module.php
        |       |   |-- Controller
        |       |   |   |-- ListController.php
        |       |   |   |-- LoginController.php
        |       |   |-- Factory
        |       |   |   |-- ListControllerFactory.php
        |       |   |-- Model
        |       |       |-- Post.php
        |       |       |-- PostCommandInterface.php
        |       |       |-- PostRepository.php
        |       |       |-- PostRepositoryInterface.php
        |       |-- view
        |           |-- blog
        |               |-- list
        |               |   |-- index.phtml
        |               |-- login
        |                   |-- login.phtml

我在博客模块工作。当我调用 LoginController.php 时,我希望它显示 login.phtml。如果我注释掉博客路由,它会起作用,但是当我取消注释博客路由时,我得到一个 'The requested URL could not be matched by routing.' 错误。

路由与显示的模板无关,因此听起来您确实遇到了路由问题。

您的路由配置结构不正确。它应该看起来更像这样:

return [
    // this line opens the configuration for the Route Manager
    'router' => [
        // open configuration for all possible routes
        'routes' => [
            // Define a new route called 'blog'
            'blog' => [
                // define 'literal' route type:
                'type' => Literal::class,
                // configure the route itself
                'options' => [
                    // listen to '/blog' as uri:
                    'route' => '/blog',
                    // define default controller and action to be called when
                    // this route is matched
                    'defaults' => [
                        'controller' => Controller\ListController::class,
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [                
                    'login' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/login',
                            'defaults' => [
                                'controller' => Controller\LoginController::class,
                                'action' => 'login',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],

差异:

  • 'login' 路由应该是子路由,而不是 blog 路由选项的一部分。
  • 我添加了 'may_terminate' => true 这使得 /blog 路由有效(默认情况下无法访问具有子路由的路由)
  • 我在登录路径中将 'route' => '/blog/login', 更改为 'route' => '/login',。由于它是博客的子项,因此您无需重复博客路径。

编辑:如果您希望两条路线都位于顶层:

return [
    // this line opens the configuration for the Route Manager
    'router' => [
        // open configuration for all possible routes
        'routes' => [
            // Define a new route called 'blog'
            'blog' => [
                // define 'literal' route type:
                'type' => Literal::class,
                // configure the route itself
                'options' => [
                    // listen to '/blog' as uri:
                    'route' => '/blog',
                    // define default controller and action to be called when
                    // this route is matched
                    'defaults' => [
                        'controller' => Controller\ListController::class,
                        'action' => 'index',
                    ],
                ],
            ],
            'login' => [
                'type' => Literal::class,
                'options' => [
                    'route' => '/login',
                    'defaults' => [
                        'controller' => Controller\LoginController::class,
                        'action' => 'login',
                    ],
                ],
            ],                
        ],
    ],

这为 /blog/login 添加了路由。如果您希望登录页面位于 /blog/login,您必须适当地编辑登录路由中的路径。