azngunit81
azngunit81

Reputation: 1604

Setting up ServiceControllerServiceProvider in Silex with YAML

Reading ServiceControllerServiceProvider http://silex.sensiolabs.org/doc/providers/service_controller.html

I wish to DI over Service Location so that my Controllers can inject Dependencies inside the constructor instead of the function.

However my Application uses YAML for routing and not PHP like the example.

This is my routing:

protected function registerRouteService()
{
    $app = $this;
    $this['routes'] = $this->extend(
        'routes',
        function (RouteCollection $routes) use ($app) {
            $loader = new YamlRouting(new FileLocator($app::$rootPath . $app::DEFAULT_ROUTES_PATH));
            $collection = $loader->load($app::DEFAULT_ROUTES_FILE);
            $routes->addCollection($collection);

            return $routes;
        }
    );
}

This function is protected because I am extending the Silex\Application in order to bootstrap up everything.

As per the document I have invoked the serviceprovider:

$app->register(new Silex\Provider\ServiceControllerServiceProvider());

My issue is that because I use YAML the following section of the document - I don't know how to apply it in general since

$app['posts.controller'] = $app->share(function() use ($app) {
    return new PostController($app['posts.repository']);
});

$app->get('/posts.json', "posts.controller:indexJsonAction");

Upvotes: 0

Views: 470

Answers (1)

Dave Marshall
Dave Marshall

Reputation: 7575

I would expect it to work just as it would with routes declared in php, the service provider doesn't really have any impact until the controller is resolved from the router, so you should be fine with

# $app::$rootPath . $app::DEFAULT_ROUTES_PATH
posts_json:
    path:      /posts.json
    defaults:  { _controller: posts.controller:indexJsonAction }

Upvotes: 1

Related Questions