Php Symfony登录-用户/管理员
我想在访问/Admin或使用登录表单时连接我的管理员 但是出了点问题,我无法访问管理员角色 (角色用户的一切都很好,也许我错过了一些管理员的事情?) 这是security.yml文件: 安全:Php Symfony登录-用户/管理员,php,security,symfony,symfony-security,Php,Security,Symfony,Symfony Security,我想在访问/Admin或使用登录表单时连接我的管理员 但是出了点问题,我无法访问管理员角色 (角色用户的一切都很好,也许我错过了一些管理员的事情?) 这是security.yml文件: 安全: providers: our_db_provider: entity: class: WebAwardsBundle:User property: username
providers:
our_db_provider:
entity:
class: WebAwardsBundle:User
property: username
# if you're using multiple entity managers
# manager_name: customer
in_memory:
memory:
users:
admin:
password: $2y$13$aabu98fd.l60phldkU.WAeDwgzqiv1IcaF.EndURJuAhGllFgzTv.
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User: bcrypt
WebAwardsBundle\Entity\User:
algorithm: bcrypt
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
#http_basic: ~
#pattern: ^/
#provider: our_db_provider
form_login:
login_path: login
check_path: login
# Log out user
logout:
path: /logout
target: /
# activate different ways to authenticate
# http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
下面是SecurityController.php文件:
class SecurityController extends Controller
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* @Route("/admin", name="admin_action")
*/
public function adminAction()
{
return new Response('<html><body>Admin page!</body></html>');
}}
类安全控制器扩展控制器
/**
*@Route(“/login”,name=“login”)
*/
公共功能登录(请求$Request)
{
$authenticationUtils=$this->get('security.authentication\u utils');
//如果存在登录错误,则获取登录错误
$error=$authenticationUtils->getLastAuthenticationError();
//用户最后输入的用户名
$lastUsername=$authenticationUtils->getLastUsername();
返回$this->render(
'login.html.twig',
排列(
//用户最后输入的用户名
“last_username”=>$lastUsername,
'error'=>$error,
)
);
}
/**
*@Route(“/admin”,name=“admin\u action”)
*/
公共职能行政行动()
{
返回新的响应('Admin page!');
}}
这是login.htm.twig文件:
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<div>CONNECTEZ-VOUS</div>
<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>
{%if错误%}
{{error.messageKey | trans(error.messageData,'security')}
{%endif%}
CONNECTEZ-VOUS
用户名:
密码:
{#
如果要控制用户的URL
成功时重定向到(更多详细信息如下)
#}
登录
如果要使用多个提供程序,需要在链中配置它们
security:
providers:
chain_provider:
chain:
providers: [our_db_provider, in_memory]
您可以阅读有关多个提供商的信息是!谢谢你@弗拉基米尔