我的Symfony项目中有两名选民。第一个选民没问题,但第二个选民有警告:
警告:get_class()期望参数1为object,给定数组为
问题出在哪里?
我的messagevoter.php:
class MessageVoter extends AbstractVoter
{
const VIEW = 'view';
const DELETE = 'delete';
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
protected function getSupportedAttributes()
{
return array(self::VIEW,self::DELETE);
}
protected function getSupportedClasses()
{
return array('App\RepairBundle\Entity\RepairMessage');
}
protected function isGranted($attribute, $message, $user = null)
{
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof UserInterface) {
return false;
}
$authChecker = $this->container->get('security.authorization_checker');
switch ($attribute) {
case self::VIEW:
if($user->getId() == $message->getUser()->getId())
{
return true;
}
break;
case self::DELETE:
if($user->getId() == $message->getUser()->getId())
{
return true;
}
break;
default:
return false;
}
}
}
我的控制器:
public function indexAction()
{
$message = $this->getDoctrine()->getRepository('RepairBundle:RepairMessage')->getMyMessage($this->get('security.token_storage')->getToken()->getUser()->getId());
$authChecker = $this->get('security.authorization_checker');
if(false === $authChecker->isGranted('view',$message))
{
throw $this->createAccessDeniedException('Unauthorized access!');
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$message,
$this->get('request')->query->get('page', 1)/*page number*/,
self::PAGE/*limit per page*/
);
$msgForm = $this->createForm(new RepairMessageType(),new RepairMessage());
return array(
'message' => $msgForm->createView(),
'pagination' => $pagination,
);
}