• Fabien Potencier's avatar
    replaced the current way to create reusable applications · c006b758
    Fabien Potencier authored
    The main idea is the move of all controller-related code from Application to
    ControllerCollection. Reusable applications are now instances of
    ControllerCollection instead of Application instances. That way, there is only
    one "true" Application instance, and thus only one Pimple container.
    
    Reusable applications can now be packaged as classes, like Extensions. It
    allows to package services and controllers into one extension.
    
    Benefits:
    
     * less hackish (proper wrapping in a class, no need to add a temporary route, no need for the LazyApplication anymore)
     * less code
     * simple and straightforward code (no magic anymore)
     * less side-effects
     * fix most severe/annoying limitations of the current implementation (from what I've read in the mailing-list and the Github issues)
     * better encapsulation of "reusable" applications
     * better separation of concerns
     * simple usage is exactly the same as before (as Application proxies the ControllerCollection methods for the "default" app)
    
    Upgrading is as simple as replacing Application with ControllerCollection for
    reusable apps (note that a reusable app is not "standalone" anymore):
    
        $mounted = new ControllerCollection();
        $mounted->get('/bar', function() { return 'foo'; });
    
        $app->mount('/foo', $mounted);
    
    A better way now is to create a class:
    
        use Silex\ApplicationExtensionInterface;
    
        class FooApplication implements ApplicationExtensionInterface
        {
            public function connect(Application $app)
            {
                $controllers = new ControllerCollection();
                $controllers->get('/bar', function() { return 'foo'; });
    
                return $controllers;
            }
        }
    
        $app->mount('/foo', new FooApplication());
    
    Note that you get the "master" application, so that you have access to the
    services defined here.
    
    If you want to register services, you can do so in the same extension:
    
        use Silex\ApplicationExtensionInterface;
        use Silex\ExtensionInterface;
    
        class FooApplication implements ApplicationExtensionInterface, ExtensionInterface
        {
            public function register(Application $app)
            {
                $app['some_service'] = ...;
            }
    
            public function connect(Application $app)
            {
                $controllers = new ControllerCollection();
                $controllers->get('/bar', function(Application $app) {
                    $app['some_service']->...;
    
                    return 'foo';
                });
    
                return $controllers;
            }
        }
    
        $ext = new FooApplication();
        $app->register($ext);
        $app->mount('/foo', $ext);
    c006b758
Name
Last commit
Last update
doc Loading commit data...
src/Silex Loading commit data...
tests Loading commit data...
vendor Loading commit data...
.gitignore Loading commit data...
.gitmodules Loading commit data...
CHANGELOG Loading commit data...
LICENSE Loading commit data...
README.md Loading commit data...
autoload.php Loading commit data...
compile Loading commit data...
example.htaccess Loading commit data...
phpunit.xml.dist Loading commit data...