Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Silex Login Form
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => [
'general' => [
'pattern' => '^/',
'anonymous' => true,
'form' => [
'login_path' => '/login',
'check_path' => '/account/login_check',
'always_use_default_target_path' => true,
'default_target_path' => '/account/home'
],
'logout' => [
'logout_path' => '/account/logout',
'target_url' => '/login',
'invalidate_session' => true
],
'users' => [
'admin' => ['ROLE_ADMIN', '$2y$10$3i9/lVd8UOFIJ6PAMFt8gu3/r5g0qeCJvoSlLCsvMTythye19F77a'],
]
]
],
'security.access_rules' => [
['^/account', 'ROLE_ADMIN']
],
'security.role_hierarchy' => [
'ROLE_ADMIN' => [
'ROLE_USER',
'ROLE_ALLOWED_TO_SWITCH'
],
]
));
$app->get('/', function (Request $request) use ($app) {
return $app['twig']->render('home/home.html.twig');
})->bind('home');
$app->get('/login', function (Request $request) use ($app) {
return $app['twig']->render('account/login.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
})->bind('login');
$app->get('/goodbye', function (Request $request) use ($app) {
return new Response("Goodbye");
});
$app->get('/account/home', function () use ($app) {
return $app['twig']->render('account/home.html.twig');
})->bind('account_home');
{% extends "base.html.twig" %}
{% block content %}
<div class="row">
<div class="col-md-12">
<div class="card text-center" style="width: 20rem;">
<div class="card-header">
Login
</div>
<div class="card-body">
<form action="{{ path('account_login_check') }}" method="post">
{{ error }}
<div class="form-group">
<label for="username">Username</label>
<input class="form-control" type="text" id="username" name="_username" value="{{ last_username }}"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" id="password" name="_password" value=""/>
</div>
<input class="btn btn-primary btn-lg" type="submit" value="Login"/>
</form>
</div>
<div class="card-footer text-muted">
<a href="#">Lost Password?</a>
</div>
</div>
</div>
</div>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment