2013-08-02 13 views
9

我不知道如何使用SecurityServiceProviderSilex。我的配置是:Silex SecurityServiceProvider引發'標識符'security.authentication_providers「未定義。'

$app['security.firewalls'] = array(
    'admin' => array(
     'pattern' => '^/_admin/.+', 
     'form' => array('login_path' => '/_admin/', 'check_path' => '/_admin/login_check'), 
     'logout' => array('logout_path' => '/_admin/logout'), 
     'users' => array(
      'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsR...'), 
     ), 
    ), 
); 
$app->register(new Silex\Provider\SecurityServiceProvider()); 

這只是拋出:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Identifier "security.authentication_providers" is not defined.' 

根據在某些情況下的文檔時,您要訪問的安全功能的請求的處理外,你必須調用$app->boot();但這不是我的情況。
如果我打電話$app->boot();$app->register(...)之前,不會引發任何異常,但它可能是因爲當時在產生登錄表單的樹枝引發不開機可言:

Unable to generate a URL for the named route "_admin_login_check" as such route does not exist. 

an issue a few months ago有可能是相同的問題,但它關閉所以我想現在應該修復了

+0

也許不相關的問題,但你不與兩個中的一個實現黃金法則(它們在文檔中陳述):您的login_path必須定義在限制區域之外。你的位於/ _admin /,而你的禁區位於/_admin/.+,所以你沒有在外界定義它。 – mTorres

+0

我不這麼認爲。表達式'/ _admin /.+'不匹配'/ _admin /'路徑,所以'/ _admin /'在禁區之外。 – martin

回答

12

我在嘗試在TwigServiceProvider之前註冊SecurityServiceProvider時遇到了同樣的異常。

我只是改變了註冊順序(安全嫩枝)和一切開始工作的優良:

// Twig service 

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => sprintf("%s/../views", __DIR__), 
)); 

// Security service 

$app["security.firewalls"] = array(); 
$app->register(new Silex\Provider\SecurityServiceProvider()); 
+1

這是因爲'TwigServiceProvider' checkes是否'$應用[「安全」]'設置其實沒有註冊Symfony的安全嫩枝擴展(如'is_granted')既然你要註冊'SecurityServiceProvider' TwigServiceProvider'後'它贏得不要將擴展名添加到Twig中。 – martin

+0

你是對的,這是一個快速解決方案,當我無法找到異常背後的原因時,我用它來啓動並運行應用程序。奎奎的答案似乎更爲優化。 – agmangas

12

你必須引導SecurityServiceProvider註冊和TwigServiceProvider註冊與您的應用程序:

// Security service 
$app["security.firewalls"] = array(); 
$app->register(new Silex\Provider\SecurityServiceProvider()); 

// Boot your application 
$app->boot(); 

// Twig service 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => sprintf("%s/../views", __DIR__), 
)); 

上面的代碼似乎是fi x您的問題,但您必須至少添加一個身份驗證提供程序。

0

我遇到了同樣的問題 - 也與當前的silex版本〜2.7。

最後我發現,在我的情況下,通過作曲家集成的「symfony/twig-bridge」組件是個問題。我整合了這種樹枝橋組件,以便在我的樹枝模板中使用特徵(例如{{ 'Age'|trans }})來使用trans特徵。從項目中刪除樹枝橋後,所有按預期工作。

爲了在我的模板使用反式我已經實現了I18nExtension爲自己仍然使用特性語法:

<?php 

namespace AppBundle\Utils; 

class I18nExtension extends \Twig_Extension { 
    private $app; 

    /** 
    * Register the extension after registering the TwigServiceProvider by 
    * $app['twig']->addExtension(new AppBundle\Utils\I18nExtension($app)); 
    */ 
    public function __construct(\Silex\Application $app) { 
     $this->app = $app; 
    } 

    /** 
    * Provide an additional simple filter called trans - calling 
    * the translate function specified below. 
    */ 
    public function getFilters() { 
     return array(
      new \Twig_SimpleFilter('trans', array($this, 'translate')), 
     ); 
    } 

    /** 
    * Translates the given $value using the translator registered in the app. 
    */ 
    public function translate($value) { 
     return $this->app['translator']->trans($value); 
    } 

    /** 
    * Name of the extension. 
    */ 
    public function getName() { 
     return "I18nExtension"; 
    } 
}