[英]Silex authentication + authorization (UserProvider, SecurityServiceProvider)
[英]Silex SecurityServiceProvider throws 'Identifier “security.authentication_providers” is not defined.'
我無法弄清楚如何在Silex
使用SecurityServiceProvider
。 我的配置是:
$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(...)
它沒有引發任何異常,但它可能根本不啟動,因為那時生成登錄表單Twig拋出:
Unable to generate a URL for the named route "_admin_login_check" as such route does not exist.
嘗試在TwigServiceProvider
之前注冊SecurityServiceProvider
時,我遇到了同樣的異常。
我剛剛更改了注冊順序( Twig 之后的 安全性 ),一切都開始正常工作:
// 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());
您必須在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__),
));
上面的代碼似乎可以解決您的問題,但您必須至少添加一個身份驗證提供程序。
我遇到了同樣的問題 - 同時使用當前的silex版本~2.7。
最后我發現,在我的情況下,通過composer集成的“symfony / twig-bridge”組件就是問題所在。 我整合了這個twig-bridge組件,使用我的twig模板中的trans特性進行翻譯(例如{{ 'Age'|trans }}
)。 從項目中刪除twig-bridge后,所有工作都按預期工作。
為了在我的模板中使用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";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.