Silex 2:具有区域设置的安全防火墙错误(Silex SecurityServiceProvider + Pmaxs \ LocaleServiceProvider) [英] Silex 2 : Security firewall error with locale (Silex SecurityServiceProvider + Pmaxs\LocaleServiceProvider)
问题描述
我看到了此解决方案,但我无法使其正常工作...
I saw this solution but I can't make it works...
所以这是我的代码,我尝试了什么,得到了什么:
So here is my code, what I tried and what I get :
1/我的安全提供者:
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'secured' => array(
'pattern' => '^/',
'anonymous' => true,
'logout' => true,
'form' => array(
'login_path' => '/login',
'check_path' => '/login_check'),
'users' => function () use ($app) {
return new Myapp\DAO\UserDAO($app['db']);
},
),
),
));
2/我的翻译提供商
我使用: https://github.com/pmaxs/silex-locale
$app->register(new Silex\Provider\TranslationServiceProvider());
$app->register(new \Pmaxs\Silex\Locale\Provider\LocaleServiceProvider(), [
'locale.locales' => ['en', 'fr'],
'locale.default_locale' => 'en',
'locale.resolve_by_host' => false,
'locale.exclude_routes' => ['^_']
]);
$app->register(new Silex\Provider\LocaleServiceProvider());
$app->extend('translator', function($translator, $app) {
$translator->addLoader('yaml', new YamlFileLoader());
// The translation file : one by langage, add new here
$translator->addResource('yaml', __DIR__.'/../src/locales/en.yml', 'en');
$translator->addResource('yaml', __DIR__.'/../src/locales/fr.yml', 'fr');
return $translator;
});
这样,我的路线没有/{_locale}/
,但是当我有/my-path
,/locale/my-path
时仍然可以使用.
This way my route have no /{_locale}/
but still works when I have /my-path
, /locale/my-path
.
3/我的登录控制器
$app->get('/login', function(Request $request) use ($app) {
return $app['twig']->render('login.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
})->bind('login');
我尝试过的-我得到的错误
- 当我的URL为
/login
时,它可以工作:我回到连接的首页. - 当我的URL为
/fr/login
时不起作用:我的URL变为/fr/login_check
并给我发送了error 404
(这是正常现象,因为我没有该URL的视图/页面,但不知道为什么会尝试显示此页面,并且不像以前那样与我建立联系. - When my URL is
/login
it works : I came back to my Home page connected. - When my URL is
/fr/login
it doesn't work : my URL became/fr/login_check
and send me aerror 404
(this is normal because I have no view / page for this URL but no idea why it tries to display this page and don't connect me as before).
What I tried - The error I get
因此,我尝试在我的安全提供程序中更改此设置:
So I tried to change this in my Security Provider:
'form' => array(
'login_path' => '/login',
'check_path' => '/{_locale}/login_check'),
它给我一个error 500 : An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "login_check" as such route does not exist.").
所以我回到了另一个安全提供程序(没有/{_locale}/
)并按照我看到的解决方案中的建议更改了登录控制器:
So I came back to my other Security Provider (without the /{_locale}/
) and change my login controller as suggested in the solution I saw :
$app->get('/{_locale}/login', function(Request $request) use ($app) {
return $app['twig']->render('login.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
})->bind('login');
- 现在,当我尝试使用默认语言环境(en)登录时,我拥有此URL
/en/login
并且可以正常工作. - 当我尝试更改法语语言时,我使用此URL
/fr/en/login
登录,然后回到URL/fr/login_check
上的error 404
. - Now when I try to log with my default locale (en), I have this URL
/en/login
and it works. - When I try to change langage for French I have this URL
/fr/en/login
to login and I came back to myerror 404
on URL/fr/login_check
.
当我尝试使用error 404
在URL /fr/logout
上的/fr/my-page
注销时遇到相同的问题...
I have the same problem when I try to LOGOUT from /fr/my-page
with a error 404
on URL /fr/logout
...
让Silex Security Provider与pmaxs/silex-locale
一起使用的任何想法吗?
Any idea to make the Silex Security Provider works with pmaxs/silex-locale
?
一个同事建议我,即使我的用户选择了另一种语言,也要在"en"中保留登录和注销的权限(因为它可以工作).
One collegue suggested me to stay in "en" for the login and logout (because it works), even if my user chose an other langage.
您认为这是个好主意吗? (我不是100%可以,因为这样我无法翻译我的内容)
Do you think it's a good idea? (I'm not 100% okay because this way I can't translate my content)
由于我对此问题一无所知,因此我尝试检查我的两条路线/login_check
和/logout
以查看是否可以更改它们,您知道在哪里可以找到它们吗?
Since I have no anwser to my question, I tried to check my two routes /login_check
and /logout
to see if I can change them, do you know where I can find them?
然后,我尝试在转到这两条路线之前进行一些重定向,但不知道我该怎么做...这个主意:
Then, I tried to make some redirection before going to those two routes, but don't know how I can do it...the idea :
// If I log from those URL, it works
/login
/en/login
// If I log from those, it doesn't works :
/fr/login
/fr/fr/login
// So from the URL that doesn't works, I'd like to do :
/fr/login ---> redirect to /login ---> execute code
/fr/fr/login ---> redirect to /en/login ---> execute code
与/logout
相同. 知道我是否可以做到以及如何做到吗?
END EDIT
PS:这是有关此项目的另一个问题,在这里我解释了如何允许用户更改网站上的语言:
PS : Here is an other question about this project where I explain how I allow user to change langage on the website : Silex : allow user to change langage by clicking on html element and keeping clean URL
推荐答案
我认为我可以通过这种方式解决问题:
I think I manage to solve my problem this way :
1/要躲避我的error 404
在我有除en
之外的其他语言环境时登录并注销的位置,我将en
中的用户重定向到check_path
和logout
之前:
1/ To dodge my error 404
where I login and logout when I have another locale than en
, I redirect the user in en
before my check_path
and logout
:
// The login form action
<form role="form" action="{{ locale_generate('en', 'login_check') }}" method="post">
// The logout
<a href="{{ locale_generate('en', 'logout') }}"> <span class="log">{{ 'logout'|trans }}</span></a>
这种方式可以正常工作,但是用户总是以英语返回,因此我必须在页面中重定向他,但是使用的语言环境与以前相同.
This way it works but user came back in english all the time, so I have to redirect him in my page but with the same locale than before.
2/所以我在会话中添加了所选语言(如果我没有$_SESSION['langue']
,则将默认语言环境用作值:en
)
2/ So I add the selected langage in session (if I have no $_SESSION['langue']
, I use my default locale as value : en
)
//===============================================
// My Ajax call where I send the selected langage value
//===============================================
$(".change_langue").on("click", function() {
var new_langue = $(this).data("lg");
$.ajax({
url: "{{ path('new-langue') }}",
type: 'POST',
data: {'langue': new_langue},
success: function (resp) {
console.log(resp);
},
error: function (resp) {
console.log(resp);
}
});
});
//========================================================
// My controller where I set the value in session
//========================================================
$app->post('/new-langue', function(Request $request) use ($app) {
$new_langue = $request->get('langue');
$app['session']->set('langue', $new_langue);
$result['new_langue'] = $app['session']->get('langue');
return new Response(json_encode($result));
})->bind('new-langue');
3/然后我在注册SecurityProvider时更改了默认的登录路径:
3/ Then I change my default login path when I registered my SecurityProvider :
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'secured' => array(
'pattern' => '^/',
'anonymous' => true,
'form' => array(
'login_path' => 'login',
'check_path' => '/login_check',
'always_use_default_target_path' => true, // I add this line
'default_target_path' => '/login/redirect' // I add this line
),
'logout' => array(
'logout_path' => '/logout',
'invalidate_session' => true,
),
'users' => function () use ($app) {
return new _mypixers_silex\DAO\UserDAO($app['db']);
},
),
),
));
4/现在,在新的/login/redirect
控制器中,我检查$_SESSION['langue']
值并重定向到正确的语言:
4/ And now in my new /login/redirect
controller I check the $_SESSION['langue']
value and redirect to the right langage :
//========================================================
// LOGIN REDIRECT
//========================================================
$app->get('/login/redirect', function(Request $request) use ($app) {
$session_langue = $app['session']->get('langue');
if (empty($session_langue)) {
$session_langue = 'en';
}
return $app->redirect($app['locale.url_generator']->generate($session_langue, 'pixers'));
})->bind('login_redirect');
这样,当我使用en
以外的其他语言环境登录或注销时,我没有更多错误,并且用户停留在当前语言中.
This way I have no more error when I login or logout with an other locale than en
and the user stay in his current langage.
PS:当我现在注销时,我仍然会回到英语,但是我正在研究一种解决方案.完成后,我将编辑我的anwser
PS : I still come back to english when I logout right now, but I'm working on a solution. I will edit my anwser when it's done
这篇关于Silex 2:具有区域设置的安全防火墙错误(Silex SecurityServiceProvider + Pmaxs \ LocaleServiceProvider)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!