将实时站点迁移到 MVC 结构的策略?

发布于 2024-12-11 01:55:13 字数 294 浏览 0 评论 0原文

SO 上有很多关于 MVC 和 MVC 入门的好内容,但我很难找到有关如何在现有的实时网站上最好地实现 MVC 结构的内容。

我的网站是一个由回声和串联 HTML 组成的令人讨厌的大杂烩,这会让任何专业程序员感到厌恶,但它确实有效。

但是,我想花一些时间来解决不断增加的技术债务,而且意味着转向更加健全的 MVC 结构。

如果可能的话,我希望避免让'er rip! 100% 重写和启动方法,而是一次一小部分。但基本控制器的集中式结构似乎不适合这样的做法?

There is a lot of good content on SO about MVC and getting started with MVC, but I'm having trouble finding anything about how best to implement MVC structure on a pre-existing, live website.

My site is a nasty mishmash of echos and concatenated HTML that would make any professional programmer throw-up, but it works.

I'd like to spend some time tackling the mounting technical debt, however, and that means moving to a much more sane MVC structure.

If at all possible, I'd like to avoid a let 'er rip! 100% rewrite and launch approach, and instead take it a section at a time. But it seems the basic controller's centralized structure is not suitable for such an approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2024-12-18 01:55:13

如果我了解该代码库的总体质量水平是多少,那么就没有办法一步迁移到 MVC。这是不可能的。另一个坏消息是框架无济于事。他们无法神奇地将糟糕的代码库转变为类似 MVC 架构的东西。

相反,您应该专注于增量重构。您的目标应该是主要遵循 SOLID 原则和 LoD。当您重构代码时,架构将自行出现。 MVC 有许多变体和风格。

您可能想要查看的一件事是在 php 中使用模板的方法。检查代码,看看必须更改哪些内容才能满足您的需求(这更多的是一个方向,而不是一个完整的解决方案)。请记住,在类似 MVC 的结构中,View 不是模板,但 View 使用多个模板。

您可能受益的另一件事是了解有关 datamappers 的更多信息。实现它们将是朝着创建模型层方向迈出的良好一步。

哦..然后有一些一般性的讲座你可以看一下(都是30分钟以上):

哦,还有这本书对重构大型php有一些见解项目。可能对你有用。

If i understand what is the overall level of quality for that codebase , then there is no way to move to MVC in one step. It is just impossible. Another bad news is that frameworks will not help. They cannot magically transform a bad codebase into something resembling MVCish architecture.

Instead you should focus on incremental refactoring. Your goal should be code that is mostly following SOLID principles and LoD. And while you refactor your code , the architecture will emerge by itself. MVC has many variants and flavors.

One definite thing you might want to look at are the ways of using templates in php. Examine the code, and see what you has to change to fit your needs (it is more of a direction, not a complete solution). And remember that in MVC-like structures View is not a template, but it View uses multiple templates.

Another thing you might benefit from is learning more about datamappers. Implementing them would be a good step in direction of creating model layer.

Oh .. and then there are few general lectures you could take a look at ( all are 30min+ ):

Oh , and this book has some insights into refactoring large php projects. Could be useful for you.

痞味浪人 2024-12-18 01:55:13

我同意这里的其他建议,框架不会是一个神奇的解决方案。

然而,从长远来看,它是有帮助的。
我现在已经将一些 Mishmash 网站转换为 Kohana 框架,并获得了以下经验。

最初我对 kohana 不太了解,所以我在重新编码我的网站时学习了这一点。我最终停止了重写并从头开始编写了一个全新的项目来学习 kohana,然后回到重写项目,现在我更好地理解了框架。

如果您不理解该框架,尝试使用它来转换旧项目将是一个陡峭的学习曲线

  1. 重写的第一步是将所有嵌入到页面中的业务/数据库逻辑拉到每个页面的顶部(在 html 输出之前)。所以我没有改变网站的流程/结构,只是将业务逻辑与显示逻辑分开。

    之后,我有了一个具有易于阅读的业务逻辑的网站,只是采用旧的结构,同时我也熟悉了旧的代码库。重写

  2. 我所做的下一步是修复所有数据库结构问题,以便所有内容都处于第三范式(如果可能)。

    我发现将旧代码修改为新数据库结构,然后在新框架中解决旧数据库结构更容易。 (kohana 很大程度上是一个基于约定的框架,而不是配置,所以遵循这些约定是很好的,以方便长期维护)

    无论框架如何,拥有良好的数据库结构都会让生活变得更轻松

  3. 下一步是选择要替换的网站。在 Kohana 中设置路线并让 Kohana 为该项目的该部分提供服务。 kohana(毫无疑问还有其他框架)有一个后备方案,如果通过 url 请求的文件已存在于网站上,那么 kohana 将不会处理该请求

    由于您已将 php 文件中的业务逻辑与显示逻辑分开,因此将代码拆分为控制器和视图就很简单了。对这两个部分进行更改以适应框架。当控制器/视图按预期工作后,您可以将业务逻辑拆分为模型/控制器

通过站点的该部分后,您可以将业务逻辑拆分为模型/控制器,直到完成。然后测试/启动/错误修复等,

然后在网站的下一部分重新开始。

最终你会到达那里......

虽然花了很多时间重写,但对我来说这是值得的,因为现在网站更容易维护。 (显然增益的大小将取决于原始代码库的质量)

祝你好运

i agree with the other suggestions here, a framework isn't going to be a magic fix.

however it can help in the long run.
i have converted a number of mishmash sites to kohana framework now, and have had the following experiences.

initially i didn't know kohana well enough, so i was learning that while recoding mysite. I ended up stopping the rewrite and coding a completely new project from scratch to learn kohana, then went back to the rewrite project, now that i understood the framework better.

if you don't understand the framework, it is going to be a steep learning curve trying to use it to convert an old project

  1. first step in the rewrite was to pull all the business/database logic embedded into the pages up to the top of each page (prior to the html output). So that i wasn't changing the flow/structure of the website, just separating business logic from display logic.

    After that i had a site that had easily readable business logic, just in the old structure, and i had familiarised myself with the old codebase at the same time.

  2. next step i did was to fix any database structure issues so that everything was in 3rd normal form (if possible).

    i found it easier to modify the old code to the new database structure, then to work around and old database structure in the new framework. (kohana is largely a convention based framework, rather then configuration, so it was nice to follow those conventions, to ease long term maintenance)

    having a good database structure makes life easier regardless of framework

  3. next step was to pick a part of the website to replace. set up the routes in kohana and let kohana serve that part of the project. kohana (and other frameworks no doubt) have a fallback, if a file being requested via a url already exists on the site, then kohana won't handle that request

    since you have separated the business logic from the display logic in your php files, it is a simple matter of splitting the code into a controller and a view. make the changes to both parts to suit the framework. you can split the business logic into model/controller after you have the controller/view working as expected

work your way through that part of the site, until complete. then test/launch/bugfix etc

then start again on the next part of the site.

eventually you will get there...

although it took a lot of time to rewrite, for me it was worthwhile, as the sites are far easier to maintain now. (obviously the amount of gain will be dependant on the quality of the original codebase)

good luck

無處可尋 2024-12-18 01:55:13

这是可以做到的。如果在请求的路径中找不到实际文件,您可以将 mod 重写为仅重定向到 boot.php 或其他内容。这将允许您一次完成一个部分。然而,确保所有链接都按顺序排列将是一场噩梦。

可能想要进行重写,并在使用时从旧应用程序中复制并粘贴您需要的部分。

it is possible to do. you can write your mod rewrites to only redirect to boot.php or whatever if no actual file is found at the requested path. this would allow you to do a section at a time. making sure all of your links are in order will be a nightmare however.

may wan to do a rewrite, and copy and paste peices you need out of the old application as you go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文