場景:Symfony2的 - 如何擴大供應商的套裝(如的FOSFacebookBundle)
我使用的是包(FOSFacebookBundle),讓我設置參數在我的配置只有一個Facebook應用程序。一切正常,但現在我需要設置不僅一個應用程序,但多個。
我的方法:
我已經創建了一個AcmeFacebookBundle,其允許定義(在的Acme \ FacebookBundle \ DependencyInjection \配置定義的配置)在陣列中,像這樣的多個應用:
acme_facebook:
apps:
some_competition:
server_url: %acme.facebook.some_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook.some_competition.app_id%
secret: % acme .facebook.some_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
some_other_competition:
server_url: %acme.facebook. some_other_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook. some_other_competition.app_id%
secret: % acme .facebook. some_other_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
In Acme \ FacebookBundle \ DependencyInjection \ AcmeFacebookExtension然後我循環遍歷所有應用程序。這個想法是將server_url參數與當前URL進行比較,並用我的配置覆蓋fos_facebook配置。
class AcmeFacebookExtension extends Extension
{
...
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config['apps'] as $app)
{
// check for matching path here?
foreach (array('file', 'app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions') as $attribute)
{
$container->setParameter('fos_facebook.' . $attribute, $app[$attribute]);
}
}
}
問題:
但是,這也正是我堅持。很明顯,我無法訪問AcmeFacebookExtension中的Request對象或DiC來進行此比較。 我的方法完全錯了嗎?你對如何解決這個問題有更好的想法嗎?
不同questin,同一個目標:Symfony2的負載條件設置](http://stackoverflow.com/questions/9603384/symfony2-load-conditional-configuration) – leek 2012-03-13 14:02:47
是的,我不確定在控制器中設置配置是否正確?當配置解析發生時注入配置感覺就像正確的方式去做... – Burgi 2012-03-13 23:11:20