为什么 Zend Framework 操作控制器有后缀?
我是 Zend Framework 的新用户,但我对 PHP 或编程并不陌生。我一直在浏览代码,试图掌握所有内容如何组合在一起。我一直在研究的部分之一是如何在系统中自动加载类。我可以看到:
- Zend_Application_Bootstrap 引用 Zend/Application/Bootstrap.php
- Zend_Controller_Action 引用 Zend/Controller/Action.php
- ... 等等
本质上 _ 转换为目录分隔符并自动加载。
我不明白(虽然我确信有一个很好的理由)是为什么动作控制器不遵循这个约定?
- IndexController 引用 Application/controllers/IndexController.php
为什么不:
- Application_Controllers_Index -> /Application/Controllers/Index.php
- 或
- Controllers_Index -> /Application/Controllers/Index.php
?
我猜想给类一个后缀会降低系统中某个地方的复杂性,从第一次看 zend 框架开始,一切都经过深思熟虑 - 我无法想象没有充分理由就引入约定。
谁能解释为什么控制器有前缀,或者更好地向我指出核心中的一些代码,显示为什么它们必须有前缀?
谢谢 :)
I am quite a new Zend Framework user but I am not new to PHP or Programming. I have been going through the code to try and get to grips with how everything slots together. One of the parts I have been looking at is how classes are Autoloaded in the system. I can see that:
- Zend_Application_Bootstrap references Zend/Application/Bootstrap.php
- Zend_Controller_Action references Zend/Controller/Action.php
- ... etc etc
Essentially _ get converted to the directory sepearator and get autoloaded.
What I don't understand (although im sure there is a good reason) is why this convention isn't followed for action controllers?
- IndexController references Application/controllers/IndexController.php
Why not:
- Application_Controllers_Index -> /Application/Controllers/Index.php
- or
- Controllers_Index -> /Application/Controllers/Index.php
?
I am guessing giving the class a suffix reduces some complexity in the system somewhere, from first looks at the zend framework everything is very well thought out - I cannot imagine conventions are introduced without good reason.
Can anyone explain why controllers are prefixed, or even better point me to some code in the core showing why they have to be prefixed?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,我并不完全确定最初制定这些约定的原因。我怀疑它们与当时其他框架的发展方式有关——特别是 RoR 的流行,这就是他们定义应用程序资源的方式。 (在最初的 MVC 迭代时,我刚刚开始使用 Zend;我在 2006 年秋天进行了重写,但目标是使其与之前开发的内容保持一致,同时提供更好、更多的功能灵活性。)
我们继续沿用这一范式,因为它强化了这样的想法:应用程序/层次结构下的所有项目都是资源,而不是您的库代码。这一点特别重要,因为您可能在此树中包含非类代码(视图脚本、布局等)。
然而,这确实带来了一些麻烦——资源加载器的引入表明我们有一个需要解决的问题。资源加载器基本上解决了症状,但不一定解决根本原因(不良约定)。当我们开发 ZF2 时,我们将重新审视这一点。如果您有兴趣发表您的想法,我邀请您在 zf-contributors 邮件列表上这样做。
To be honest, I'm not entirely certain as to why the conventions were originally developed. I suspect they had to do with how other frameworks were evolving at the time -- in particular, RoR was spiking in popularity, and this was how they defined application resources. (At the time of the original MVC iteration, I was just starting at Zend; I did a rewrite in fall of 2006, but the goal of that was to keep it as consistent with what had been developed previously, while simultaneously offering better and more flexibility.)
We've continued along the paragigm as it reinforces the idea that all items under the application/ hierarchy are resources, and not your library code. This is particularly important due to the fact that you may have non-class code within this tree (view scripts, layouts, etc.).
However, this has certainly made for a few headaches -- the introduction of the resource loader shows that we had a problem we needed to resolve. The resource loader basically addresses the symptoms, but not necessarily the root cause (poor conventions). As we work on ZF2, this is something we will be revisiting. If you're interested in posting your thoughts, I invite you to do so on the zf-contributors mailing list.
控制器的文件名和类名应以控制器为后缀。 Zend 在类名中看到一个后缀,并理解它是一个控制器。他在controllers文件夹中找到了这个类。
File names and classes names of Controllers should has a postfix Controller. Zend see a postfix in class name, and understand, that it is a controller. He finds this class in controllers folder.
我自己对 Zend Framework 还很陌生,但据我所知,鉴于 Zend Framework 目前的情况,没有充分的理由这样做。我不确定这是否是事情开始时的遗留问题,或者只是相关人员的偏好,但我听说 ZF 2.0 将会对此进行更改。
I'm only fairly new to Zend Framework myself, but from what I can tell, given the way the Zend Framework currently stands there is no good reason for this. I'm not sure if it's a hangover from the way things started out, or just the preference of people involved, but I've heard this will be changed for ZF 2.0.