2017-03-03 49 views
0

我使用Symfony的CMF的配置價值,它有一個RoutingAutoBundle,暴露出這些參數:Symfony的 - 更改動態

cmf_routing_auto: 
    .... 
    persistence: 
     phpcr: 
      route_basepath: /routes 

我需要動態設置route_basepath在旅途中,是有可能改變的配置。 yml值是否動態?

我正在構建一個爲每個用戶隔離的CMS,爲此,我將每個用戶的路由存儲在他自己的PHPCR文檔中。

我的想法是根據請求HTTP_HOST和或子域在早期請求偵聽器中更改路由器基本路徑。

EDIT

這裏是結構。

enter image description here

+0

您應該爲您的特定環境創建配置。如config_subdomain。然後在您的AppKernel中加載所需的環境。 – fyrye

+0

用戶和包含到該特定站點的路由的「站點」文檔將隨時創建,因此這將是一個壞主意,必須進行編輯。 –

+0

您可以使用一個參數或多個參數,但是您的應用程序需要知道所需'routes'作爲要包含在config.yml中的參數的位置。 EG:'route_basepath:'%kernel.user_routes%''我爲''自己使用'%router.request_context.host%',但是我的應用程序通過配置被隔離。 – fyrye

回答

0

其他任何人試圖做到這一點,這是當你是新來的CMF包和自動佈線時,route_basepath一個相當複雜的任務,不能動態的事件監聽器等,改變,如果你依賴於該請求,該請求在該點將不可用。

我設法通過執行以下操作做到這一點:

覆蓋「cmf_routing.phpcr_candidates_prefix」的服務,並用自己的候選人前綴服務替換所有引用。

class OverrideRoutePrefixListener implements CompilerPassInterface 
{ 
    public function process(ContainerBuilder $container) 
    { 
     $routePrefixDefinition = $container->getDefinition('cmf_routing.phpcrodm_route_idprefix_listener'); 
     $routePrefixDefinition->replaceArgument(0, new Reference('app.phpcr_candidates_prefix')); 

     $routeLocalePrefixDefinition = $container->getDefinition('cmf_routing.phpcrodm_route_locale_listener'); 
     $routeLocalePrefixDefinition->replaceArgument(0, new Reference('app.phpcr_candidates_prefix')); 
    } 
} 

自己的服務應該繼承原和更改構造函數,你可能要複製一大堆私人領域等

public function __construct(array $prefixes, array $locales = array(), ManagerRegistry $doctrine = null, $limit = 20) 
{ 
    // Do something else here, if you just want multiple prefixes, its supported by the config 
    // But if you want to fetch them from the database or the like, you have to do this. 
    $prefixes = ['/users/admin/sites/test/routes', '/users/admin/sites/test/simple']; 

    parent::__construct($prefixes, $locales = array(), $doctrine = null, $limit = 20); 
    $this->setPrefixes($prefixes); 

    $this->doctrine = $doctrine; 
} 

裏面自己cmf_routing.phpcr_candidates_prefix的,你是自由的解決前綴到任何你想要的。

第二步是重寫PhpcrOdmAdapter,你可以用編譯器通過。

class OverridePhpcrOdmAdapter implements CompilerPassInterface 
{ 
    public function process(ContainerBuilder $container) 
    { 
     $definition = $container->getDefinition('cmf_routing_auto.adapter.phpcr_odm'); 
     $definition->setClass(PhpcrOdmAdapter::class); 
    } 
} 

然後改變構造函數的基路徑。