【问题标题】:Migrating custom Legacy PHP web applications to Laravel将自定义的旧版 PHP Web 应用程序迁移到 Laravel
【发布时间】:2020-03-11 22:23:52
【问题描述】:

我正在尝试按照本指南前往Migrating Legacy Web Applications to Laravel

这是我的情况:

我在同一主机下有 2 个 Web 应用程序,都是用 PHP 编写的

  • 一个(旧版)是“自定义框架”
  • 第二个是 Laravel 5.4 应用程序

我的目标是仅在 Laravel 应用程序下为这两个应用程序提供服务,因此我对该应用程序的唯一入口点是来自 Laravel 的 index.php

正如您在文章中看到的那样,我正在尝试在 Laravel App\Exceptions\Handler 中启动旧版应用程序

public function render($request, Exception $exception) {
   if( $exception instanceof NotFoundHttpException ) {
      // pass to legacy framework - contents of index.php
      die();
   }
}

当没有找到旧路由时。

我创建了自己的App\Providers\LegacyServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class LegacyServiceProvider extends ServiceProvider
{
    protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton("LegacyApp", function ($app){
            require_once base_path("legacy/index.php");
            return;
        });
    }
}

config/app.php laravel 配置文件中添加

    // other providers
    App\Providers\LegacyServiceProvider::class,


现在有趣的部分来了...这是旧版应用程序

index.php
<?php
require_once __DIR__ ."/app/config/conf.php";

$oSession = new Session();
$oDb = new DbMedoo();
$dbCon = $oDb->dbConn();

include __DIR__ . '/app/routes.php';

app/config/conf.php

<?php

// Some definitions here

// ROUTING
$router = new AltoRouter();

// TWIG
$loader = new Twig_Loader_Filesystem(SITE_CONFIG_DIR . "../../src/App/views");

// Other stuff here

app/routes.php 文件

<?php
// **************************************************
// ****************    AltoRouter   *****************
// **************************************************

$router->map( 'GET|POST', '/', 'home.php', 'home');

// Some other legacy routes here

最后是一个简单的home.php“控制器”

<?php

// Some initial checks here

$someModelClass = new SomeModelClass();
$anotherModelClass = new AnotherModelClass();

// some other logics here

$template = $twig->loadTemplate('home.twig');

echo $template->render(array(
    'someVariables' => $someVariableToPass,
));

如你所见,我没有班级!!

故事结束!

所以,当我尝试在App\Exception\Handler 中运行旧版应用程序时

if( $exception instanceof NotFoundHttpException ) {
    // pass to legacy framework
    app()->make("LegacyApp");
    die(); // prevent Laravel sending a 404 response
}

这个错误恰如其分地出现

(1/1) ReflectionException
Class LegacyApp does not exist

欢迎提出建议

这里有同样情况的人吗?我如何在不重构整个代码库的情况下做到这一点?

谢谢你的建议

【问题讨论】:

    标签: php laravel laravel-5 legacy-code


    【解决方案1】:

    我会说你不能。并且不要使用“重构”这个词。使用这个词,“重写”。因为这就是你将对一个正在运行和移动货物的应用程序执行的操作,即使它不在“Laravel”中。

    我预计财务上的理由几乎肯定不存在。

    【讨论】:

    • 我同意。这是我的遗产,我无法弄清楚如何在不失去理智的情况下解雇。
    猜你喜欢
    • 2010-10-08
    • 2011-12-02
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多