zend 中的模型命名
如果我不遵循模型名称的命名结构(例如模型文件夹中 Users.php 中的 Application_Model_DbTable_Users ),我会在控制器上收到错误。我不想将其写为 Application_Model_DbTable_Users ,而是将其简单地写为模型文件夹中 Users.php 中的 Users 。我知道这在 zend 中是可以实现的,但是如何实现?我不知道。请帮助解决这个问题。我想必须在 bootstrap.php 或 index.php 中做一些事情来配置自动加载器......无论确切的解决方案是什么......但请帮助我解决这个问题。 提前致谢
If i m not following the naming structure for the model name ( eg Application_Model_DbTable_Users in Users.php in model folder), i m getting error on controller. instead of writing it as Application_Model_DbTable_Users , i want to write it simply as Users in Users.php in model folder. i know this is achievable in zend but how ?? i dont know. Please help in this concern. i guess something must be done in bootstrap.php or index.php for configuring autoloader.... whatever is the exact solution..... but please help me out for this.
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你怎么知道这是可能的? Zend Framework 有严格的命名语法,能够自动加载所有文件。自动加载器使用前缀(例如
Application_Model_
)来确定文件所在的位置。如果没有前缀,这是不可能的。有时,在解释 ZF 行为时,一直输入
Application_Model_User
的工作量太大,为了更清楚起见,使用了User
等内容。这并不意味着您可以在应用程序中使用此名称。最后一种可能性是名称空间,但在稳定的 ZF 中尚未使用它。
How do you know this is possible? The Zend Framework has a strict naming syntax to be able to load all files automatically. The autoloader uses the prefixes (like
Application_Model_
) to determine where the file is located. Without the prefix, this is not possible.Sometimes during explanation of ZF's behaviour, it's too much work to type all the time
Application_Model_User
and instead of it, to make it more clear, things likeUser
are used. This does not mean you can use this name in your application.A last possibility is namespaces, but in the stable ZF it isn't used yet.
ZF 遵循 PEAR 命名约定,文件夹层次结构对应于类的命名,例如 Foo_Bar_Baz 将位于文件夹 Foo/Bar/Baz.php 中。自动加载器将尝试查找相对于配置的包含路径的此路径。
这意味着,您可以将所有类放入自动加载器已知的单个顶级目录中,或者放置额外的包含路径,或者 - 这是更好的选择 - 将额外的自动加载器添加到知道如何映射类名的默认自动加载器到文件。
详细信息请参阅ZF 自动加载参考指南。
ZF follows PEAR naming convention in that the folder hierarchy corresponds to the naming of the classes, e.g. Foo_Bar_Baz will be in the folder Foo/Bar/Baz.php. The autoloader will try to find this path relative to the configured include path.
This means, you can either put all your classes into a single top level directory already known by the Autoloader or put an additional include path or - and that's the better option - add an additional autoloader to the default autoloader that knows how to map class names to files.
Please refer to the ZF Reference Guide on Autoloading for details.
您可以实现一个自定义自动加载器,它只需在类名前面加上模型路径即可。
在 Zend_Loader 的文档中有一个关于如何构建您自己的自动加载器插件。
基本上只是一个搜索&替换任务。
但是,我强烈建议不要使用这种方法,因为您的全局命名空间会受到严重污染。
至少使用一个简单的前缀(如 Model_)。
you could implement an custom autoloader which simply prepends the classname with the model path.
in the documentation of the Zend_Loader there is an example on how to build your own autoloader plugin.
basically just an search & replace task.
However, i strongly recommend not using this approach, as your global namespace would be polluted a lot.
at least use a simple prefix (like Model_).