mercredi 11 mars 2020

Migrating custom Legacy PHP web applications to Laravel

I'm trying to follow this guide to Migrating Legacy Web Applications to Laravel.

This is my situation:

I've 2 web application under the same host, both written in PHP

  • one (the legacy) is a "custom framework"
  • second is a Laravel 5.4 application

My goal is to serve both the application only under Laravel app, so my unique entry point for the application is the index.php from the Laravel.

As you can read in the article, I'm trying to start the legacy app inside the Laravel App\Exceptions\Handler

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

when the legacy route is not found.

I've created my own 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;
        });
    }
}

addend in the config/app.php laravel configuration file

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


and now comes the fun part... This is the index.php of the legacy app

<?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

the app/routes.php file

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

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

// Some other legacy routes here

and finally a simple home.php "controller"

<?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,
));

As you can see I've no Class!!

END OF STORY!

So, when i try to run the legacy app in the App\Exception\Handler

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

This error fittingly appears

(1/1) ReflectionException
Class LegacyApp does not exist

Suggestions are welcomed

Someone with the same situation here? How I could do this without refactor the entire codebase?

Thank you in advice



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire