我不明白模块

时间:2018-06-01 00:01:44

标签: zend-framework zend-framework3

我正在尝试学习Zend Framework 3,我已经完成了专辑教程和另一个博客教程。我已经写了大约3年的直接PHP应用程序,并使用Slim 3编写了2个Web应用程序。我试图围绕模块。我不会放弃了解模块是什么。我理解模块的方式是,模块是主应用程序内部的一个小应用程序。我试图在同一个模块中创建2个控制器,并从这些控制器渲染不同的视图。我整天搜索过,找不到创建两个控制器的方法,让它们路由到不同的视图。但是,我发现了很多应用程序有5个不同模块的例子,所有模块都只有一个控制器,也许是类方法所指向的不同视图。我的理解是应该为每个页面创建一个模块吗?例如,登录模块和about模块以及联系人模块?

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

我正在使用Blog模块。当我调用LoginController.php时,我希望它显示login.phtml。如果我评论出博客路线它会起作用,但是当我取消注释博客路线时,我得到一个'请求的URL无法通过路由匹配'。错误。

1 个答案:

答案 0 :(得分:2)

路由与显示的模板无关,因此听起来确实存在路由问题。

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

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,则必须适当地编辑登录路径中的路径。