Zend framework 我不知道';我不懂模块
我正在尝试学习Zend Framework 3,我已经完成了专辑教程和另一个博客教程。我已经写了3年的php应用程序,并且用Slim3写了2个web应用程序。我正试着把我的头绕在模块上。我不明白什么是模块。我对模块的理解是,模块是主应用程序中的一个小应用程序。我试图在同一个模块中创建两个控制器,并从这些控制器渲染不同的视图。我搜索了一整天,找不到一种方法来创建两个控制器并将它们路由到不同的视图。但是,我发现了很多例子,一个应用程序有5个不同的模块,所有模块都只有一个控制器,可能还有一个类方法指向的不同视图。我的理解是应该为每个页面创建一个模块吗?例如,登录模块、关于模块和联系模块 module.config.phpZend framework 我不知道';我不懂模块,zend-framework,zend-framework3,Zend Framework,Zend Framework3,我正在尝试学习Zend Framework 3,我已经完成了专辑教程和另一个博客教程。我已经写了3年的php应用程序,并且用Slim3写了2个web应用程序。我正试着把我的头绕在模块上。我不明白什么是模块。我对模块的理解是,模块是主应用程序中的一个小应用程序。我试图在同一个模块中创建两个控制器,并从这些控制器渲染不同的视图。我搜索了一整天,找不到一种方法来创建两个控制器并将它们路由到不同的视图。但是,我发现了很多例子,一个应用程序有5个不同的模块,所有模块都只有一个控制器,可能还有一个类方法指向
<?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'
],
]
];
我在博客模块工作。当我调用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'
路由选项的一部分李>博客
- 我添加了
,这使得'may_terminate'=>true
路由工作(默认情况下,带有子路由的路由不可访问)/blog
- 我在登录路径中将
更改为'route'=>'/blog/login',
。因为它是blog的子对象,所以不需要重复blog路径'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
,则必须适当编辑登录路径。默认情况下,控制器将呈现不同的视图,因为默认模板名称基于控制器名称。您正在做的哪些工作不起作用?因此,在“主”路线之后的任何路线都是子路线?不,在另一条路线的child_routes
中定义的路线都是子路线。其他任何东西都是顶级路线。我可以在同一个模块中有两个顶级路线吗?当然,你可以根据自己的喜好组织路线。我在回答中添加了第二个示例。
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',
],
],
],
],
],