简体   繁体   English

Zend Framework:Controller类== Page?

[英]Zend Framework: Controller class == Page?

Am I right in thinking that in Zend Framework, if I plan to have 5 pages at my site, I'd generally need 5 controllers? 我是否正确地认为在Zend Framework中,如果我计划在我的网站上有5个页面,我通常需要5个控制器? Do ZF developers typically create 1 controller per page ("page" as abtract app unit)? ZF开发人员通常每页创建1个控制器(“页面”作为abtract app unit)?

The reason I am asking this is that previously for some reason I stuffed a lot of various actions into controllers so that they played role of pages, eg index/add, index/view, index/delete and displayed various small screens, eg small CRUD screens as opposed to grids displayed by index action. 我问这个的原因是之前由于某种原因,我把很多不同的动作塞进控制器,以便他们扮演页面的角色,例如索引/添加,索引/视图,索引/删除和显示各种小屏幕,例如小CRUD屏幕而不是索引操作显示的网格。

But as of now I want to check my new understanding that actions are needed mostly for model updates and actions should, once run, immediately redirect back to controller/index. 但是到目前为止,我想检查一下我的新理解,即操作主要用于模型更新,操作一旦运行,应立即重定向回控制器/索引。 So it seems that views should mostly be used in index actions, and less frequently in other actions. 因此,看起来应该主要在索引操作中使用视图,而在其他操作中则不太常见。

Does this sound architecturally valid? 这听起来在架构上有效吗?

I suppose thats one way of doing it... Using your analogy i suppose i use a controller like a "section" and actions like "pages" to those sections. 我认为这是一种方式...使用你的类比我想我使用像“部分”这样的控制器和那些部分的“页面”之类的操作。

But thats getting stuck more on the old procedural||static paradigm of pages because really the action controller itself has no more to do with a page or section than the association you make as a developer. 但是那更多地依赖于旧的程序性||静态范例页面,因为实际上动作控制器本身与页面或部分没有关系,而不是你作为开发人员所做的关联。 Ie. IE浏览器。 each page can potentially be a composite of numerous actions and views depening on the application's architecture. 每个页面都可能是依赖于应用程序架构的众多操作和视图的组合。

For example if i have a blog modules i might have the following: 例如,如果我有一个博客模块,我可能会有以下内容:

Controllers: 控制器:

  • PostController PostController中
  • CommentController CommentController

Views: 浏览次数:

  • post 岗位
    • display (display a single post) 显示(显示单个帖子)
    • list (list posts) 列表(列表帖子)
    • edit (create/edit post) 编辑(创建/编辑帖子)
  • comment 评论
    • display (display n comments) 显示(显示n条评论)
    • list (list comments) 清单(清单评论)
    • edit (create/edit/moderate comment) 编辑(创建/编辑/审核评论)
    • embed-list (for use on the Post itself assuming this differs from display) 嵌入列表(用于Post本身,假设这与显示不同)

In my opinion, a controller should always group multiple methods that have a common use case. 在我看来,控制器应该总是将多个具有共同用例的方法组合在一起。 Try to find "nouns", that describe the functionality of your app: those are the controllers (while verbs are the controller methods). 尝试找到描述应用程序功能的“名词”:那些是控制器(动词是控制器方法)。

If you have (for example) a blog, you may have: 如果你有(例如)博客,你可能有:

  • ArticleListController (listing all articles, listing the top 10, listing all articles for a tag,...) ArticleListController(列出所有文章,列出前10名,列出标签的所有文章,......)
  • SingleArticleController (displaying one article, adding comments,...) SingleArticleController(显示一篇文章,添加评论,......)
  • AdminController (lets you write Articles) AdminController(允许你写文章)

If you have just 5 pages (eg home, about, contact,...) the MVC architecture might not be the right fit. 如果你只有5页(例如家庭,关于,联系......),MVC架构可能不合适。

indexAction is merely the default action for a given controller, useful for displaying a default view (the same as displaying the index.html or index.php page for a sub-directory). indexAction仅是给定控制器的默认操作,对于显示默认视图很有用(与显示子目录的index.html或index.php页面相同)。

It is completely acceptable to create a variety of actions on a per controller basis that may or may not each have their own views ( ie add and edit). 在每个控制器的基础上创建各种动作是完全可以接受的,每个动作可能有也可能没有自己的视图( 添加和编辑)。 The controller is intended to group similar actions. 控制器旨在对类似的操作进行分组。

For example usage please refer to the documentation on Zend_Controller_Action . 有关示例用法,请参阅Zend_Controller_Action上的文档。 You may also find it useful to familiarize yourself with the workflow of Zend_Controller . 您可能还会发现熟悉Zend_Controller的工作流程很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM