我正在尝试学习 Zend Framework 3,并且我已经完成了专辑教程和另一个博客教程。我已经编写直接 php 应用程序大约 3 年了,并使用 Slim 3 编写了 2 个 Web 应用程序。我正在尝试围绕模块进行思考。我不明白什么是模块。我理解模块的方式是,模块是主应用程序内部的一个小应用程序。我试图在同一模块中创建 2 个 Controller ,并从这些 Controller 渲染不同的 View 。我搜索了一整天,找不到创建两个 Controller 并将它们路由到不同 View 的方法。但是,我发现了许多示例,一个应用程序有 5 个不同的模块,所有模块都只有一个 Controller ,并且类方法可能指向不同的 View 。我的理解是应该为每个页面创建一个模块吗?例如,登录模块和关于模块和联系模块?
模块.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。如果我注释掉博客路由,它将起作用,但是当我取消注释博客路由时,我会收到“路由无法匹配请求的 URL”。错误。
最佳答案
路由与显示的模板无关,因此听起来您确实存在路由问题。
您的路由配置结构不正确。它应该看起来更像这样:
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
您必须适本地编辑登录路由中的路径。
关于zend-framework - 我不懂模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50634380/