Symfony2 multiple config and routing files for sub

2019-04-11 11:06发布

We are building a Symfony2 application that will serve different sections using subdomains:

  1. api.tld.com - API system
  2. docs.tld.com - Documentation
  3. assets.tld.com - System for serving images

How we are doing this is creating an app directory for each subdomain, and keeping the standard /app directory in place as the central shared config. There is also a custom bootstrap in the web directory for each app. Subdomains are routed accordingly using .htaccess.

The problem I am having is where multiple config files come in, particularly when they have their own routing imports. In some cases, there can be up to 4 configs.yml files. Take the following URL for example:

http://testing.docs.tld.com

The config setup currently works like this (and it works)

  1. tld.com - Global config located at /app/config/config.yml
  2. testing - Environment config located at /app/config/config_testing.yml. This config also imports config_dev.yml in the same directory.
  3. docs - App config located at /app_docs/config/config.yml

These are all imported in the AppKernal in /app_docs/AppKernal.php:

// Load Global Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config.yml');

// Load Environment Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config_' . $this->getEnvironment() . '.yml');

// Load App-centric Configuration
$loader->load(__DIR__.'/config/config.yml');

Now the configs load just fine. But what I'm having trouble with, and not found any definitive documentation on, is when more than one of these configs define framework: router: resources. In the above example configs, these are loaded (attempted to anyway) as follows:

/app/config/config.yml

framework:
    secret:%secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: %kernel.debug%

/app/config/config_testing.yml

// No special Routing

/app/config/config_dev.yml

framework:
    router:   { resource: "%kernel.root_dir%/config/routing_dev.yml" }

/app_docs/config/config.yml

framework:
    secret: %secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: %kernel.debug%

All of the configs are loading fine. But what I found is that only the last routing file called above is being included. So I assume the rule is that they are overriden as a rule, rather than extended.

So what I have spent the last couple of days trying to find out is, is it possible to extend the inclusion of routing files within config files in the fashion above? Another option I investigated was to find a way to import routing files in the AppKernal files. I was only able to find this, which doesn't explain exactly at what point this should be used (or where). It doesn't work within the AppKernal where the configs are included, so I assume the Router is not active at that stage.

Anyone have any ideas? I'd be very grateful.

1条回答
爷、活的狠高调
2楼-- · 2019-04-11 11:27

I had the same need so we did like this:

/apps/config
/apps/config/common_config.yml
/apps/config/common_routing.yml
/apps/config/...

/apps/myapp1
/apps/myapp1/myapp1Kernel.php
/apps/myapp1/...
/apps/myapp1/config
/apps/myapp1/config/config.yml
/apps/myapp1/config/routing.yml
/apps/myapp1/config/...

/apps/myapp2
/apps/myapp2/myapp1Kernel.php
/apps/myapp2/...
/apps/myapp2/config
/apps/myapp2/config/config.yml
/apps/myapp2/config/routing.yml
/apps/myapp2/config/...

...

And in each app's yml file, we had:

/apps/myapp1/config/config.yml

imports:
    - { resource: "../../config/common_config.yml" }

And then, you have to reproduce the same way in /web

/web/myapp1/app.php

Who will be calling your app

$kernel = new myapp1Kernel('prod', false);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
查看更多
登录 后发表回答