Symfony2 用于子域路由的多个配置和路由文件 [英] Symfony2 multiple config and routing files for subdomain routing

查看:26
本文介绍了Symfony2 用于子域路由的多个配置和路由文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个 Symfony2 应用程序,它将使用子域为不同的部分提供服务:

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

  1. api.tld.com - API 系统
  2. docs.tld.com - 文档
  3. assets.tld.com - 提供图像的系统

我们的做法是为每个子域创建一个应用程序目录,并将标准/app 目录作为中央共享配置保留.每个应用程序的 web 目录中还有一个自定义引导程序.使用 .htaccess 相应地路由子域.

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.

我遇到的问题是多个配置文件进来的地方,特别是当它们有自己的路由导入时.在某些情况下,最多可以有 4 个 configs.yml 文件.以下面的网址为例:

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 - 位于/app/config/config.yml 的全局配置
  2. testing - 环境配置位于/app/config/config_testing.yml.此配置还会在同一目录中导入 config_dev.yml.
  3. docs - 位于/app_docs/config/config.yml 的应用配置

这些都是在/app_docs/AppKernal.php中的AppKernal中导入的:

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.

所以我在过去几天里试图找出的是,是否可以以上述方式在配置文件中扩展路由文件的包含?我调查的另一个选择是找到一种方法来导入 AppKernal 文件中的路由文件.我只能找到this,这并没有准确解释应该在什么时候(或在哪里)使用它.它在包含配置的 AppKernal 中不起作用,因此我假设路由器在该阶段未处于活动状态.

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.

推荐答案

我有同样的需求,所以我们这样做了:

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/...

...

在每个应用的 yml 文件中,我们有:

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

/apps/myapp1/config/config.yml

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

然后,你必须在/web 中以同样的方式重现

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

/web/myapp1/app.php

谁将调用您的应用

$kernel = new myapp1Kernel('prod', false);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

这篇关于Symfony2 用于子域路由的多个配置和路由文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆