default locale

This commit is contained in:
Simon Vieille 2015-11-24 18:57:06 +01:00
parent a2daa58502
commit ca4246d3af
7 changed files with 46 additions and 13 deletions

View file

@ -42,10 +42,10 @@ $app['routes'] = $app->share($app->extend('routes', function ($routes, $app) {
*/
$app->get('/', function (Request $request) use ($app) {
$accept = AcceptHeader::fromString($request->headers->get('Accept-Language'));
$cookie = $request->cookies->get('locale');
$cookieValue = $request->cookies->get('locale');
if (!empty($cookie) && in_array($cookie, $app['locales'])) {
$foundLocale = $cookie;
if (!empty($cookieValue) && in_array($cookieValue, $app['locales'])) {
$foundLocale = $cookieValue;
} else {
$foundLocale = $app['translator']->getLocale();
@ -64,6 +64,8 @@ $app->get('/', function (Request $request) use ($app) {
});
$app->after(function(Request $request, Response $response) use ($app) {
$cookie = new Cookie('locale', $request->attributes->get('_locale'), strtotime('+1 month'));
$value = $request->cookies->get('locale');
$cookie = new Cookie('locale', $value, strtotime('+1 month'));
$response->headers->setCookie($cookie);
});

View file

@ -3,10 +3,11 @@
use Gist\Service\UserProvider;
use Silex\Provider\SecurityServiceProvider;
use Gist\Service\SaltGenerator;
use Silex\Provider\SessionServiceProvider;
use Gist\Security\AuthenticationProvider;
use Gist\Security\AuthenticationListener;
use Gist\Security\AuthenticationEntryPoint;
use Gist\Security\LogoutSuccessHandler;
use Silex\Provider\SessionServiceProvider;
use Symfony\Component\Security\Http\HttpUtils;
$app['enable_registration'] = true;
@ -25,7 +26,6 @@ $app['user.provider'] = $app->share(function ($app) {
$app->register(new SessionServiceProvider());
$app['security.authentication_listener.factory.form'] = $app->protect(function ($name, $options) use ($app) {
$app['security.authentication_provider.'.$name.'.form'] = $app->share(function ($app) {
return new AuthenticationProvider($app['user.provider']);
@ -37,7 +37,7 @@ $app['security.authentication_listener.factory.form'] = $app->protect(function (
$app['security.authentication_provider.'.$name.'.form']
);
});
return [
'security.authentication_provider.'.$name.'.form',
'security.authentication_listener.'.$name.'.form',
@ -45,7 +45,7 @@ $app['security.authentication_listener.factory.form'] = $app->protect(function (
'pre_auth'
];
});
$app->register(
new SecurityServiceProvider(),
[
@ -56,12 +56,11 @@ $app->register(
'form' => [
'login_path' => '_login',
'check_path' => '/login_check',
'always_use_default_target_path' => true,
'always_use_default_target_path' => false,
'default_target_path' => '/',
],
'logout' => [
'path' => '/logout',
'target' => '/',
],
'users' => $app->share(function () use ($app) {
return $app['user.provider'];
@ -73,3 +72,12 @@ $app->register(
]
]
);
$app['security.authentication.logout_handler._proto'] = $app->protect(function ($name, $options) use ($app) {
return $app->share(function () use ($name, $options, $app) {
return new LogoutSuccessHandler(
$app['security.http_utils'],
isset($options['target_url']) ? $options['target_url'] : '/'
);
});
});

View file

@ -8,6 +8,7 @@ use Gist\Form\UserRegisterForm;
use Gist\Form\UserLoginForm;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class LoginController

View file

@ -12,8 +12,6 @@ class MyController extends Controller
{
public function myAction(Request $request)
{
$app = $this->getApp();
return $this->render('My/my.html.twig');
}
}

View file

@ -34,6 +34,8 @@
<p>
<input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}">
</p>
<input type="hidden" name="_target_path" value="{{ path('my') }}" />
</div>
</div>
</div>

View file

@ -44,7 +44,7 @@
</a>
</li>
<li>
<a href="{{ path('logout') }}">
<a href="{{ path('logout', {target_url: path('home')}) }}">
{{ 'app.menu.my.logout.title'|trans }}
</a>
</li>

View file

@ -0,0 +1,22 @@
<?php
namespace Gist\Security;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class LogoutSuccessHandler
* @author Simon Vieille <simon@deblan.fr>
*/
class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
public function onLogoutSuccess(Request $request)
{
$targetUrl = $request->query->get('target_url') ? $request->query->get('target_url') : '/';
return new RedirectResponse($targetUrl);
}
}