将自定义的旧版PHP Web应用程序迁移到Laravel

时间:2020-03-11 14:19:40

标签: php laravel laravel-5 legacy-code

我正在尝试按照本指南操作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

欢迎提出建议

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

谢谢你的建议

1 个答案:

答案 0 :(得分:0)

我会说你不能。而且不要使用“重构”一词。使用“ rewrite”一词。因为这是对现在正在运行并移动货物的应用程序所做的操作,即使它不在“ Laravel”中。

我预计几乎没有财务理由。

相关问题