将非框架 PHP 项目移植到 Laravel 4.x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23467424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:28:47  来源:igfitidea点击:

Porting a non-framework PHP project to Laravel 4.x

phplaravelframeworkslaravel-4

提问by Matteo-SoftNet

I have a big PHP project in which I didn't use any framework (nor MVC pattern), and I want to gradually port it on Laravel with (almost) no downtime.
I'm searching for ways of making transparent the migration for pieces of code already ported to Laravel, while mantaining functional older plain PHP code.

For example, Laravel "overwrites" superglobals ($_GET, $_POST, etc.) using classes and methods, (ex. Input::get()). My plain PHP project of course makes direct use of superglobals. How can I overcome these "uncompatibilities" under Laravel without having to rewrite instantly all my PHP code?

Otherwise, if you think this task being too difficult, is there any PHP framework that, thanks to its internal structure, would make this task easier?

我有一个很大的 PHP 项目,我没有使用任何框架(也没有使用 MVC 模式),我想逐渐将它移植到 Laravel 上,(几乎)没有停机时间。
我正在寻找使已经移植到 Laravel 的代码片段的迁移透明化的方法,同时维护功能性较旧的纯 PHP 代码。

例如,Laravel 使用类和方法(例如 Input::get())“覆盖”超全局变量($_GET、$_POST 等)。我的普通 PHP 项目当然直接使用了超全局变量。如何在 Laravel 下克服这些“不兼容性”而不必立即重写我的所有 PHP 代码?

否则,如果您认为这项任务太难了,是否有任何 PHP 框架可以通过其内部结构使这项任务更容易?

UPDATE:
The use of superglobals is still possible in laravel. I was getting a sneaky error: Laravel internally sets error_reporting to E_ALL and shows a custom error stack trace page even for PHP E_NOTICE, but without explicitly specifying the error level(that it is a NOTICE error), even if this was done by default by PHP error reporting message engine.

Let me say, for Laravel core developers, that I consider this "partially silent" behaviour as generally misleading for any PHP developer trying to port his code on to their framework.

更新:
在 laravel 中仍然可以使用超全局变量。我收到了一个偷偷摸摸的错误:Laravel 在内部将 error_reporting 设置为 E_ALL 并显示自定义错误堆栈跟踪页面,即使是 PHP E_NOTICE,但没有明确指定错误级别(这是一个 NOTICE 错误),即使这是默认情况下完成的PHP 错误报告消息引擎。

让我说,对于 Laravel 核心开发人员,我认为这种“部分沉默”的行为通常会误导任何试图将其代码移植到他们的框架的 PHP 开发人员。

回答by tere?ko

TL;DR

TL; 博士

Don't do this.

不要这样做。

Common misconception

常见的误解

If you have an existing project, then grafting it on a framework will gain you no benefits. Frameworks are notsome magic sauce, which miraculously makes the code better or make the site run faster.

如果你有一个现有的项目,那么将它嫁接到一个框架上不会给你带来任何好处。框架是不是有些神奇的调料,它奇迹般地使代码更好或使网站运行得更快。

Instead, frameworks are tools, that (supposedly) help you write the project in a shorter amount of time by having the "common tasks" already done. Also, as a side effect, it is extremely difficult to stop using a framework in a project (if it, for example, gets discontinued), because all of your code now is tightly welded to a framework.

相反,框架是工具,它(据说)通过已经完成“常见任务”来帮助您在更短的时间内编写项目。此外,作为一个副作用,在项目中停止使用框架是极其困难的(例如,如果它停止使用),因为您的所有代码现在都与框架紧密相连。

This is why you are currently having these problems:

这就是您目前遇到这些问题的原因:

  • How can I overcome these "uncompatibilities" under Laravel without having to rewrite instantly all my PHP code?

    You cannot. Rewrite it exactly as the Laravel framework wants you to or it won't work.

  • (..) is there any PHP framework that, thanks to its internal structure, would make this task easier?

    No, there are not. All the popular frameworks will want you to rewrite your code so that it is bound to a framework.

  • 如何在 Laravel 下克服这些“不兼容性”而不必立即重写我的所有 PHP 代码?

    你不能。完全按照 Laravel 框架的要求重写它,否则它将无法工作。

  • (..) 是否有任何 PHP 框架,由于其内部结构,可以使这项任务更容易?

    不是,没有。所有流行的框架都希望您重写代码,以便将其绑定到框架。

The better approach

更好的方法

But you already have a working application. You will have better results, if you focus on improving the existing code base:

但是您已经有了一个可以运行的应用程序。如果您专注于改进现有代码库,您将获得更好的结果:

  • make sure you follow SOLID principles
  • separate your business logic from you presentation and database abstraction
  • start adding unit tests for the really shadyparts of your code
  • refactor, refactor, refactor
  • 确保您遵循SOLID 原则
  • 将您的业务逻辑与您的表示和数据库抽象分开
  • 开始为代码中真正阴暗的部分添加单元测试
  • 重构,重构,重构

P.S.:the materials listed herewold probably help

PS:这里列出的材料可能会有所帮助