kagux / KaguxSilexIntegrationBundle

Symfony2 Bundle to integrate Silex project
7 stars 0 forks source link

KaguxSilexIntegrationBundle

Introduction

Silex is a great framework for small application. Overall simplicity, elegancy of routing and power of underlying symfony components are playing key roles here.

However, what if you started a project with a few features in mind, but it has grown a lot more since then? Services declarations now take a few hundred lines. Controllers are bloated even though you separate them into mounted controllers. You find yourself reimplementing providers for most symfony2 components and bundles. And now you wish you had started with symfony2 all along.

One of the possible solutions is to completely port application to symfony2. But if you don't want to put on hold development of your application, this route is rather difficult and time consuming.

This bundle solves the problem by seamlessly integrating Silex application into Symfony2.

Here's what you get:

Installation

        "require": { 
         "php": ">=5.3.3",
         "silex/silex": "1.0.*",
         "symfony/symfony": "2.1.*",        
         "doctrine/orm": ">=2.2.3,<2.4-dev",        
         "doctrine/doctrine-bundle": "1.0.*",
         "twig/extensions": "1.0.*",        
         "symfony/assetic-bundle": "2.1.*",        
         "symfony/swiftmailer-bundle": "2.1.*",        
         "symfony/monolog-bundle": "2.1.*",        
         "sensio/distribution-bundle": "2.1.*",        
         "sensio/framework-extra-bundle": "2.1.*",        
         "sensio/generator-bundle": "2.1.*",
         "jms/security-extra-bundle": "1.2.*",
         "jms/di-extra-bundle": "1.1.*",
         "kagux/symfony2-silex-integration-bundle": "dev-master"
        }
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Kagux\SilexIntegrationBundle\KaguxSilexIntegrationBundle()
        );

        ....

    } 
namespace Kagux\LegacyAppBundle\Application;

use Silex\Application;

class ApplicationFactory
{
    /**
     * @throws \Exception
     * @return \Silex\Application
     */
    public function create()
    {
        $app=new Application;
        $app->get('/silex', function() use ($app) {
            return 'Hello, world!';
        });
        return $app;
    }

}
parameters:
 silex.application.class:  Silex\Application
 silex.application.factory.class: Kagux\LegacyAppBundle\Application\ApplicationFactory

services:
  silex.application.factory:
    class: %silex.application.factory.class%

  legacy.silex.application:
    class: %silex.application.class%
    factory_service: silex.application.factory
    factory_method: create
  kagux_silex_integration:
   app_service: legacy.silex.application
  legacy_silex_application:
    resource: .
    type: silex
kagux_legacy_app:
    resource: "@KaguxLegacyAppBundle/Resources/config/routing.yml"
    prefix:   /

That should be it. At this point, if you browse your_site.com/silex you will see 'Hello, world!'.

TODO

Contrubuting