如何从外部遗留系统访问 Symfony2 服务? [英] How can I access Symfony2 services from an external legacy system?

查看:26
本文介绍了如何从外部遗留系统访问 Symfony2 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在缓慢地将当前系统迁移到 Symfony2,但大部分代码库仍处于内部框架中.我想从旧的遗留框架中的类中利用一些在 Symfony2 中内置的功能.是否有一种简单的方法可以从 Symfony 框架之外访问 Symfony2 服务?

We are slowly migrating our current system to Symfony2, but the majority of the codebase is still in an inhouse framework. I want to leverage some functionality built in Symfony2 from within classes in the old legacy framework. Is there an easy way to access a Symfony2 service from outside of the Symfony framework?

class MyOldClass extends SomethingOld
{
    public function getSomethingViaSymfony()
    {
        $service = new SomeSymfonyService();
        $results = $service->getResults();
    }
}

我的假设是这将是失败的,因为不会注入依赖项.

My assumption is that this would be a failure, for the dependencies wouldn't be injected.

推荐答案

你需要初始化 symfony 而不分派任何动作.这基本上意味着从 web/index.php 中的 symfony 前端控制器文件中获取代码,稍微修改它并将其粘贴到旧应用程序的某个初始化文件中.

you would need to initialize symfony without dispatching any actions. this basicly means taking the code from the symfony front controller file in web/index.php, modifying it a bit and paste it to some initialization file of your legacy app.

// legacy/init.php
require_once 'PATH_TO_SYMFONY/app/bootstrap.php.cache';
require_once 'PATH_TO_SYMFONY/app/AppKernel.php';


$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();

$GLOBALS['service_container'] = $kernel->getContainer();

请注意,此代码未经过测试,但我很确定它会起作用,因为 symfony 很棒;)

note that this code is not tested but i'm pretty sure it'll work because symfony is great;)

你也可以考虑一种不同的策略,将遗留动作嵌入到 symfony 中,而不是相反.

also you could think about a different strategy in which you embed the legacy action into symfony and not the other way around.

您必须实现一个旧控制器并在路由定义的末尾为其提供一个捕获所有路由.此控制器可以初始化遗留应用程序并向其分派操作.之后你可以在路由文件的顶部依次定义新的路由并用symfony调度它们.

you would have to implement a legacy controller and give it a catch all route at the end of the routing definition. this controller can initialize the legacy application and dispatch actions to it. afterwards you can successively define new routes at the top of the routing file and dispatch them with symfony.

恕我直言,这种策略要好得多,因为您可以几乎不改动遗留应用程序,然后一块一块地杀死它.

this strategy is imho much better because you can leave the legacy application almost untouched and kill it piece by piece.

这篇关于如何从外部遗留系统访问 Symfony2 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆