在添加用户navbar的索引页面时,我在 Symfony 4.2.2 项目的TraceableVoter构造函数中有一个TypeError
。 Security.yaml文件附加到下面的此问题。
所有项目源都是Udemy提供的Symfony 4课程的完整副本。
TypeError
具有以下回溯:
传递给Symfony \ Component \ Security \ Core \ Event \ VoteEvent :: __ construct()的参数4必须为整数类型,给定为null,在/ home / vagrant / symfony-01 / vendor / symfony / security-中调用第40行的core / Authorization / Voter / TraceableVoter.php Twig_Environment-> render('micro-post / index.html.twig',array('posts'=> array(object(MicroPost),object(MicroPost),object(MicroPost),object(MicroPost),object(MicroPost) ,对象(MicroPost),对象(MicroPost),对象(MicroPost),对象(MicroPost),对象(MicroPost)))) 在src / Controller / MicroPostController.php中(第81行)
intex.html的导航栏
<nav class="navbar navbar-dark navbar-expand-md sticky-top bg-dark p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0"
href="{{ path('micro_post_index') }}">Micro Post App</a>
<div class="w-100 order-1 order-md-0">
<ul class="navbar-nav px-3">
{% if is_granted('ROLE_USER') %}
<li class="nav-item text-nowrap">
<a class="nav-link" href="{{ path('micro_post_add') }}">Add new</a>
</li>
{% endif %}
</ul>
</div>
<div class="order-2 order-md-1">
<ul class="navbar-nav px-3">
{% if is_granted('ROLE_USER') %}
<li class="nav-item text-nowrap">
<a class="nav-link"
href="#">{{ app.user.fullName }}</a>
</li>
<li class="nav-item text-nowrap">
<a class="nav-link"
href="{{ path('security_logout') }}">Sign out</a>
</li>
{% else %}
<li class="nav-item text-nowrap">
<a class="nav-link"
href="{{ path('security_login') }}">Sign in</a>
</li>
{% endif %}
</ul>
</div>
</nav>
MicroPostController.php中的处理索引页面
/**
* @Route("/", name="micro_post_index")
* */
public function index()
{
$html = $this->twig->render('micro-post/index.html.twig', ['posts' => $this->microPostRepository->findAll()]);
return new Response($html);
}
security.yaml
security:
encoders:
App\Entity\User:
algorithm: bcrypt
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
pattern: ^/
form_login:
check_path: security_login
login_path: security_login
csrf_token_generator: security.csrf.token_manager
default_target_path: micro_post_index
logout:
path: security_logout
target: micro_post_index
access_control:
- { path: ^/micro-post, roles: ROLE_USER }
这不是重复的问题。转换角色toArray()
不能解决我的问题。
这是完整的堆栈跟踪:
TypeError:
Argument 4 passed to Symfony\Component\Security\Core\Event\VoteEvent::__construct() must be of the type integer, null given, called in /home/vagrant/symfony-01/vendor/symfony/security-core/Authorization/Voter/TraceableVoter.php on line 40
at vendor/symfony/security-core/Event/VoteEvent.php:31
at Symfony\Component\Security\Core\Event\VoteEvent->__construct(object(ExampleVoter), object(Request), array('ROLE_USER'), null)
(vendor/symfony/security-core/Authorization/Voter/TraceableVoter.php:40)
at Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter->vote(object(AnonymousToken), object(Request), array('ROLE_USER'))
(vendor/symfony/security-core/Authorization/AccessDecisionManager.php:73)
at Symfony\Component\Security\Core\Authorization\AccessDecisionManager->decideAffirmative(object(AnonymousToken), array('ROLE_USER'), object(Request))
(vendor/symfony/security-core/Authorization/AccessDecisionManager.php:60)
at Symfony\Component\Security\Core\Authorization\AccessDecisionManager->decide(object(AnonymousToken), array('ROLE_USER'), object(Request))
(vendor/symfony/security-core/Authorization/TraceableAccessDecisionManager.php:61)
at Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager->decide(object(AnonymousToken), array('ROLE_USER'), object(Request))
(vendor/symfony/security-http/Firewall/AccessListener.php:67)
at Symfony\Component\Security\Http\Firewall\AccessListener->handle(object(GetResponseEvent))
(vendor/symfony/security-bundle/Debug/WrappedListener.php:46)
at Symfony\Bundle\SecurityBundle\Debug\WrappedListener->handle(object(GetResponseEvent))
(vendor/symfony/security-bundle/Debug/TraceableFirewallListener.php:35)
at Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener->handleRequest(object(GetResponseEvent), object(Generator))
(vendor/symfony/security-http/Firewall.php:90)
at Symfony\Component\Security\Http\Firewall->onKernelRequest(object(GetResponseEvent))
(vendor/symfony/security-bundle/EventListener/FirewallListener.php:48)
at Symfony\Bundle\SecurityBundle\EventListener\FirewallListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
(vendor/symfony/event-dispatcher/Debug/WrappedListener.php:111)
at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
(vendor/symfony/event-dispatcher/EventDispatcher.php:212)
at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent))
(vendor/symfony/event-dispatcher/EventDispatcher.php:44)
at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
(vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:145)
at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
(vendor/symfony/http-kernel/HttpKernel.php:126)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:67)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:198)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:25)
答案 0 :(得分:2)
关于MicroPostController.php(索引操作),有一些说明,
$this->render('path_to_template)
;而不是$this->twig->render('path_to_template)
您不必将Twig视图传递给任何变量并返回Response()
如果要在Symfony 4.x中使用 实体存储库 ,请使用 依赖项注入 ,因此您的 索引 需要像public这样的参数
function index(MicroPostRepository $microPostRepository) {}
然后您的控制器必须是这样
/**
* @Route("/", name="micro_post_index")
* */
public function index(MicroPostRepository $microPostRepository)
{
return $this->render('micro-post/index.html.twig', [
'posts' => $microPostRepository->findAll()
]);
}
最后,要解决该问题,请检查您的用户实体,并将您的 角色获取器 替换为
public function getRoles()
{
return $this->roles->toArray();
}
要创建用户实体,最佳做法是使用 制造商捆绑包 进行操作,并避免出现此错误
php bin/console make:user
答案 1 :(得分:0)
给出了app.js的错误路径-build\js\app.js
。
通过向基本模板添加有效的app.js
路径来解决:
base.html.twig
...
{% block javascripts %}
<script src="{{ asset('build/app.js') }}"></script>
{% endblock %}
</body>
答案 2 :(得分:0)
我有同样的问题。 $result
方法中有TraceableVoter->vote
个变量。它应该返回一个整数。我转储了这个变量,确实有int 0
。但是出于某些原因,VoteEvent
类将此值当作null。因此,在TraceableVoter
中的第38行之后,我添加了intval($result);
。
public function vote(TokenInterface $token, $subject, array $attributes)
{
$result = $this->voter->vote($token, $subject, $attributes);
$result = intval($result);//here
$this->eventDispatcher->dispatch('debug.security.authorization.vote', new VoteEvent($this->voter, $subject, $attributes, $result));
return $result;
}
我是symfony的新手,所以我不知道我的决定是否正确,但这是可行的。
答案 3 :(得分:0)
请确保您没有选民,而选民中的功能没有回报。
当您的投票过程的结果为空时,会发生此错误。
答案 4 :(得分:0)
我认为 Alex_B 是正确的。问题是选民-它返回null。如果您在第43课(is_granted)上,则您的ExampleVoter带有空方法投票。所以我就这样使它返回0的false:
App \ Security \ ExampleVoter.php
namespace App\Security;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class ExampleVoter implements VoterInterface
{
public function vote(TokenInterface $token, $subject, array $attributes)
{
return false;
}
}
,一切恢复正常。我也是symfony的新手,希望它能解决您的问题。
答案 5 :(得分:0)
在默认的symfony设置中,可能存在对VoterInterface的替代,您可以在src / Security / ExampleVoter.php中找到它
public function vote(TokenInterface $token, $subject, array $attributes)
{
// TODO: Implement vote() method.
}
这是返回0的问题;或删除文件夹以停止覆盖。