Php 对布尔函数调用成员函数getRole()
登录时出现以下错误:Php 对布尔函数调用成员函数getRole(),php,symfony,Php,Symfony,登录时出现以下错误: FatalThrowableError in RoleHierarchy.php line 43: Call to a member function getRole() on boolean in RoleHierarchy.php line 43 at RoleHierarchy->getReachableRoles(array(false)) in RoleHierarchyVoter.php line 39 at RoleHierar
FatalThrowableError in RoleHierarchy.php line 43: Call to a member function getRole() on boolean
in RoleHierarchy.php line 43
at RoleHierarchy->getReachableRoles(array(false)) in RoleHierarchyVoter.php line 39
at RoleHierarchyVoter->extractRoles(object(UsernamePasswordToken)) in RoleVoter.php line 42
at RoleVoter->vote(object(UsernamePasswordToken), object(Request), array('IS_AUTHENTICATED_ANONYMOUSLY')) in classes.php line 4857
at AccessDecisionManager->decideAffirmative(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in classes.php line 4851
at AccessDecisionManager->decide(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in DebugAccessDecisionManager.php line 48
at DebugAccessDecisionManager->decide(object(UsernamePasswordToken), array('IS_AUTHENTICATED_ANONYMOUSLY'), object(Request)) in AccessListener.php line 69
at AccessListener->handle(object(GetResponseEvent)) in classes.php line 5044
at Firewall->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
at call_user_func(array(object(Firewall), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher)) in WrappedListener.php line 106
at WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher))
at call_user_func(object(WrappedListener), object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher)) in classes.php line 2923
at EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent)) in classes.php line 2838
at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in TraceableEventDispatcher.php line 136
at TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in classes.php line 4273
at HttpKernel->handleRaw(object(Request), 1) in classes.php line 4243
at HttpKernel->handle(object(Request), 1, true) in Kernel.php line 168
at Kernel->handle(object(Request)) in app_dev.php line 28
at require('/home/owner/Desktop/Workspace/Documentary/web/app_dev.php') in router_dev.php line 40
security.yml
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:
encoders:
DW\UserBundle\Entity\User:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
main:
entity:
class: DW\UserBundle\Entity\User
property: username
in_memory:
memory: ~
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: ~
form_login:
login_path: login
check_path: login_check
default_target_path: /
always_use_default_target_path: true
logout:
path: logout
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/connect/facebook, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
使用者
更改
public function getRoles()
{
return $this->roles->toArray();
}
到
<?php
namespace DW\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Role\RoleInterface;
class Role implements RoleInterface
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $role;
/**
* @var ArrayCollection|User[]
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* @param string $role
*/
public function setRole(string $role)
{
$this->role = $role;
}
/**
* @return ArrayCollection|User[]
*/
public function getUsers()
{
return $this->users;
}
/**
* @param ArrayCollection|User[] $users
*/
public function setUsers($users)
{
$this->users = $users;
}
}
public function getRoles()
{
return $this->roles->toArray();
}
public function getRoles()
{
$roleNames = [];
foreach ($this->roles as $role) {
$roleNames[] = $role->getRole();
}
return $roleNames;
}