如何在旧版应用程序中使用Symfony 2容器 [英] How to use the Symfony 2 Container in a legacy app

查看:50
本文介绍了如何在旧版应用程序中使用Symfony 2容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望将旧版应用程序与Symfony 2应用程序集成-用Symfony组件替换旧应用程序的越来越多的部分。我将采用的方法是在旧版应用程序中使用Symfony 2容器,以获取已为Symfony 2应用程序配置的服务。我要使用的第一个服务是会话和安全上下文。

Would like to integrate a legacy application with a Symfony 2 application - replacing more and more parts of the old application with Symfony components. The approach I would take is using the Symfony 2 container in the legacy application getting the services that are already configured for the Symfony 2 application. The first services I would like to use are the session and the security context.

问题:


  • 这可行吗?

  • 如何获取已配置的服务容器?

旧版应用程序中的更多信息:典型的PHP混乱:单个PHP文件,作为控制器(检查$ _GET和$ _POST是否有不同的执行路径)。每个页面都包含 init.php 来设置自动加载,数据库连接等。会话管理具有自己的类(我想替换),可通过调用检索数据到数据库对象的静态方法(!)。

More info in the legacy application: The typical PHP mess: Single PHP files, as "controllers" (checking $_GET and $_POST for different execution paths). Each page includes init.php which sets up autoloading, database connection etc. The session management has its own class (which i would like to replace), the data is retrieved through calls to static methods (!) of database objects.

推荐答案

使用Symfony的DIC作为独立组件 ,但是您必须手动执行许多操作(因为您没有计划一开始就使用完整的Symfony框架)。可能不会在所有旧有的东西上使用DIC。

Using Symfony's DIC as a standalone component is possible but you'd have to do many things "manually" (as you're not planning on using full Symfony Framework from the very beginning). You'll probably won't get much of using DIC with all that legacy stuff.

如果您想走这条路,我会考虑首先选择其他组件(例如HttpFoundation和HttpKernel)。

If you want to go this path I'd consider choosing another component first (like HttpFoundation and HttpKernel).

正如@Cerad建议的那样,您可以将旧代码包装在Symfony中。看看 IngewikkeldWrapperBundle 包。您无法按原样使用它,但是它可能会给您一些想法。

As @Cerad suggested you might wrap your legacy code in Symfony. Have a look at IngewikkeldWrapperBundle bundle. You can't use it as is but it might give you some ideas.

还有第三种方法。

您可以决定在Symfony应用中实施所有新功能。然后,您可以使旧版和Symfony应用程序共存。在服务器级别(即Nginx),您可以将旧版URL代理到旧版应用程序,并将所有迁移的URL代理到Symfony2应用程序。就我而言,这种情况是最好的选择,并且证明是可行的。但是,我们致力于放弃旧版应用程序开发(因此,每个新功能或更改都必须在Symfony2应用程序中开发)。

You can decide to implement every new feature in a Symfony app. Than, you can make that both legacy and Symfony apps coexist. On a server level (i.e. Nginx), you might proxy legacy URLs to the legacy app and all the migrated URLs to a Symfony2 app. In my case this scenario was the best option and proved to be working. However, we were committed to abandon legacy app development (so every new feature or change had to be developed in a Symfony2 app).

编辑:这是引导启动程序的方法。旧版应用中的Symfony内核并调度事件(防火墙需要此事件):

here's how you could boot the Symfony kernel in a legacy app and dispatch an event (which is needed for the firewall):

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

$request = Request::createFromGlobals();
$request->attributes->set('is_legacy', true);
$request->server->set('SCRIPT_FILENAME', 'app.php');

$container = $kernel->getContainer();
$container->enterScope('request');
$container->get('request_stack')->push($request);
$container->set('request', $request);

$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$eventDispatcher = $container->get('event_dispatcher');
$eventDispatcher->dispatch('kernel.request', $event);

这篇关于如何在旧版应用程序中使用Symfony 2容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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