在多站点应用程序中开发网页
我正在 Kohana 中使用其模板系统开发多个网站,以便一个代码库使用相同的后端数据库运行多个网站。我开始在视图中看到很多 if 语句来说明该网站是否执行此操作,如果该网站执行该操作。它开始看起来非常非 MVC 或面向对象。其他开发人员在决定将视图分解为单独的部分视图时是否遵循任何规则?我想重用尽可能多的代码,但不要在每个视图中都使用 if 语句。任何参考也很棒。
I'm developing several web sites in Kohana using its template system so that one code base is running several web sites using the same back end DB. I'm starting to see a lot of if statements in the views to say if this site do this, if that site do that. It starting to look very non-MVC or Object Oriented. Do other developers have any rules they follow when deciding to breakout view into separate partial views? I want to reuse as much of the code as possible but not swim in if statement in every view. Any reference would be great also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Kohana,您应该使用模块来处理您不想为每个应用程序重复的内容。然后,您可以使用扩展类或配置文件中的特定设置来保留应用程序中的细节。
http://kohanaframework.org/3.2/guide/kohana/modules
很多
if
语句主要表明您需要进行重构。允许加载不同站点的大型级联if
语句是不好的做法,因为您创建文件 紧密耦合导致当您需要进行简单的添加或更改时编辑多个文件。此外,如果每个站点都需要加载不同的依赖项或设置或 if 语句中的任何内容,它最终会变得丑陋。在不查看代码的情况下很难说出需要更改什么,但请尝试查看诸如 factory< 之类的设计模式< /a> 或 抽象工厂 设计模式创建站点对象。
一本涉及 PHP 模式和最佳实践主题的好书是 Matt Zandstra 的《PHP 5:对象、模式和实践》:http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804。一本非常好的书。
If you are using Kohana, you should use modules for stuff you don't want to duplicate for every application. Then you can keep specifics in your application with extended classes or specific settings in config files.
http://kohanaframework.org/3.2/guide/kohana/modules
A lot of
if
statements are mostly an indication that you need to do refactoring. A large cascadingif
statement to allow for different sites to be loaded is bad practice in the sense that you make files tightly coupled causing to edit multiple files when you need to make a simple addition or change. Furthermore, it will eventually become ugly if every site needs to load different dependencies or settings or whatever in your if statement.It's difficult to say what you need to change without seeing the code, but try looking at design patterns like the factory or abstract factory design patterns to create site objects.
A good book that deals with the subject of patterns and best practices with PHP is PHP 5: Objects, Patterns and Practice by Matt Zandstra: http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804. A very good book.
我遇到了类似的问题,为了保持视图的美观和整洁,我将视图类扩展为主题类。我会给工厂方法两个(或更多)视图,即 Theme::factory(array('site1/home','default/home'),$data)->bind(...);如果存在主题视图,它将使用该视图,否则仅提供默认值。这样我或其他能力较差的人就可以轻松覆盖以显示结构和视图行为。
班级看起来像这样
I had a similar issue cropping up with this, to keep the views nice and neat I extended the view class into a theme class. I would give the factory method two (or more) views ie Theme::factory(array('site1/home','default/home'),$data)->bind(...); If a themed view existed it would use that, else just serve up the default. That way I, or someone else less competent could easily override to display structure and view behaviour.
The class looks like this
您确实想要 ViewModel,只是您还不知道如何命名它们。小鸟们一直在推特上表示它将在 Kohana 3.3 中出现,但除非你有一年左右的时间,否则我建议尝试 Zombor 的视图模型模块 或放弃 Kohana 的模板系统,转而使用 KOstache (这是 Kohana 的 Mustache)
如果出于任何原因你想坚持使用当前的代码库,我会将视图分成几个较小的可互换部分并在必要时加载它们(echo View::Factory(($ Something == 'one' ? 'view_one' : 'view_two')) 或自定义的书面助手将是一个不错的玩具)
You really want ViewModels, only you don't know how to name them yet. Little birds have been tweeting that it'll be in Kohana 3.3 but unless you have a year or so to wait, I recommend trying either View-Model module by Zombor or moving away from Kohana's templating system whatsoever in favour of KOstache (it's Mustache for Kohana)
If for any reason you want to stick with your current codebase, I'd go with splitting the view into several smaller interchangable parts and loading them when necessary (echo View::Factory(($something == 'one' ? 'view_one' : 'view_two')) or a custom written helper would be a nice toy here)