dtqjbbr5283 2014-06-02 00:00
浏览 24
已采纳

Flat PHP Legacy App - 如何包装在silex路由中

I have been tasked with finding a solution to wrapping a custom made application in the silex framework as they are going to continue forward using silex. The dilemma is the legacy application is a flat php style with no controllers or models, and php with mysql queries embedded within the via files.

I have been struggling to find any clean solution to wrap the legacy app in the routing of silex to allow for new portions to be done in a controller based setyp instead of flat php. I have been checking for some time between stack overflow and other Google results, but they an del seem to end up specifying ways of doing default routes with a legacy app that has a controller based setup.

For good measure, the legacy app does use session variables so the solution must allow for those to be used.

Any and all help is appreciated.

Before people ask, I have looked at Routing in Silex/Symfony - Providing a default route and it is similar to how I would like to do it, but I need to make it work with the flat php app, not legacy controllers.

  • 写回答

3条回答 默认 最新

  • doujianjian2060 2014-11-04 16:01
    关注

    The solution ended up being a 2 part process.

    1. Place the legacy code within the web root of the silex application.
    2. Get the silex application to store sessions that are accessible by both Silex, and the legacy application.

    Code Placement

    By placing the legacy code into the web folder, anything in the URL that matches a file will go to that file instead of looking for a route. This allows for not needing to create routes for all of the old code, as it is not controller based in our situation and needs to be handled differently than most of the other restructuring that has been referred to on this site.

    Placing the code into the web directory also means that we can continue to make updates and changes to the old code, while writing new Silex based code to replace it in our available time.

    Sessions

    When it came to using the same sessions, the option we went with was....less than desirable, but it allows for us to continue with our plans without hindering the use of either application. The current plan is to implement database stored sessions once we have completed migrating the application's code to Silex.

    We went with an option first identified in this post Symfony session avoid _sf2_attributes. This is quite an ugly solution, but allows for the flexibility we need in attempting to migrate the application over in time with minimal effort up front. The goal is to migrate it over completely to the new Silex application, but the timeframe is over a year or more to do so.

    Here is how the session is configured in our Silex application. It is using file based storage.

    $app->register(new Silex\Provider\SessionServiceProvider(), array(
      'cookie_lifetime' => 86400,
    ));
    $app['session.storage'] = $app->share(function () use ($app) {
      return new \Symfony\Component\HttpFoundation\Session\Storage\LegacySessionStorage;
    });
    

    Here is a copy of the controller code located originally here, in case it is removed at some point.

    <?php
    
    use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
    
    /**
     * Session sotrage that avoids using _sf2_attributes subkey
     * in the $_SESSION superglobal but instead it uses
     * the root variable.
     */
    class LegacySessionStorage extends NativeSessionStorage 
    {
    const SYMFONY_SESSION_SUBKEY = '_sf2_attributes';
    
    /**
     * @inheritdoc
     */
        protected function loadSession(array &$session = null)
        {
            if (null === $session) {
                $session = &$_SESSION;
            }
    
            parent::loadSession($session);
    
            foreach ($this->bags as $bag) {
                $key = $bag->getStorageKey();
    
                if (self::SYMFONY_SESSION_SUBKEY === $key)
                {
                    $bag->initialize($session);
                }
            }
        }
    }
    

    I hope this helps some others in allowing them to migrate to a new coding style from an old application that is a thorn in their side. Wanted to make sure to sum up our findings over time in order to ensure others don't have to look as much in the future hopefully.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000