конфиг / пакеты / security.yaml:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# ...
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
main:
anonymous: ~
form_login:
login_path: login
check_path: login
# ...
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_ADMIN }
SRC / Controller / SecurityController.php:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
SRC / контроллер / DefaultController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends Controller
{
/**
* @Route("/admin")
*/
public function admin()
{
return new Response('<html><body>Admin page!</body></html>');
}
}
шаблоны / безопасность / login.html.twig:
{% extends 'base.html.twig' %}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
Я хочу загрузить страницу / admin или / login, я получаю сообщение об ошибке:
InvalidConfigurationException Нераспознанная опция "провайдеры" в
"Security.encoders.App \ Entity \ User"
at ArrayNode-> normalizeValue (массив ('provider' =>
array ('our_db_provider' => array ('entity' => array ('class' =>
'App \ Entity \ User', 'property' => 'email', 'firewalls' =>
array ('main' => array ('anonymous' => null, 'form_login' =>
массив ('login_path' => 'login', 'check_path' => 'login',
'access_control' => array (array ('path' => '^ / login', 'role' =>
'IS_AUTHENTICATED_ANONYMOUSLY'), массив ('path' => '^ /', 'role' =>
'ROLE_ADMIN')))))))))) в строке BaseNode.php 368