【发布时间】: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