树枝过滤器上的静态调用
Static call on a twig filter
我正在尝试获得一个可以按分数对实体进行排序的树枝过滤器。我的城市 class 获得了带有 getter 和 setter 的分数属性,我创建了这个扩展:
<?php
namespace AOFVH\HomepageBundle\Twig;
use Twig_Extension, Twig_SimpleFilter, Twig_Environment;
class ScoreExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
$filter = new Twig_SimpleFilter('score', array('AOFVH\FlyBundle\Entity\City', 'getScore'))
);
}
public function getName()
{
return 'score_extension';
}
}
我这样称呼它:
{% for cit in cities|score %}
<a href="{{ path('aofvh_city', {'name': cit.name}) }}">
<div class="col-lg-4 col-md-12" style="margin-bottom:10px;">
<img src="{{ asset('Cities/'~cit.name~'.png') }}" class="img" alt="Cinque Terre" width="300" height="300">
<h2>{{cit.name}}</h2>
</div>
</a>
{% endfor %}
但是由于某种原因,我无法渲染它,而是 git 这个错误
ContextErrorException: Runtime Notice: call_user_func_array() expects parameter 1 to be a valid callback, non-static method AOFVH\FlyBundle\Entity\City::getScore() should not be called statically
我是不是漏掉了什么?
Twig_SimpleFilter
的构造函数的第二个参数需要 callable
。
如果你给它传递一个 class 名称和一个方法名称,它将静态调用 class 的方法:
array('SomeClass', 'someMethod')
相反,如果您向它传递一个 class 的实例和一个方法名称,它将在对象内部调用该方法:
array($this->someInstance, 'someMethod')
这意味着您要么使 getScore()
静态化,要么创建 City
的实例并使用它(也许使用 依赖注入 来获取它)。
你将不得不做这样的事情,使用被过滤的实际变量。在这里,我猜 cities
是一个集合:
class ScoreExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
$filter = new Twig_SimpleFilter('score', array($this, 'getCityScore'))
);
}
public function getCityScore($cities)
{
$scores = array();
foreach ($cities as $city) {
$scores[] = $city->getScore();
}
return $scores;
}
public function getName()
{
return 'score_extension';
}
}
我正在尝试获得一个可以按分数对实体进行排序的树枝过滤器。我的城市 class 获得了带有 getter 和 setter 的分数属性,我创建了这个扩展:
<?php
namespace AOFVH\HomepageBundle\Twig;
use Twig_Extension, Twig_SimpleFilter, Twig_Environment;
class ScoreExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
$filter = new Twig_SimpleFilter('score', array('AOFVH\FlyBundle\Entity\City', 'getScore'))
);
}
public function getName()
{
return 'score_extension';
}
}
我这样称呼它:
{% for cit in cities|score %}
<a href="{{ path('aofvh_city', {'name': cit.name}) }}">
<div class="col-lg-4 col-md-12" style="margin-bottom:10px;">
<img src="{{ asset('Cities/'~cit.name~'.png') }}" class="img" alt="Cinque Terre" width="300" height="300">
<h2>{{cit.name}}</h2>
</div>
</a>
{% endfor %}
但是由于某种原因,我无法渲染它,而是 git 这个错误
ContextErrorException: Runtime Notice: call_user_func_array() expects parameter 1 to be a valid callback, non-static method AOFVH\FlyBundle\Entity\City::getScore() should not be called statically
我是不是漏掉了什么?
Twig_SimpleFilter
的构造函数的第二个参数需要 callable
。
如果你给它传递一个 class 名称和一个方法名称,它将静态调用 class 的方法:
array('SomeClass', 'someMethod')
相反,如果您向它传递一个 class 的实例和一个方法名称,它将在对象内部调用该方法:
array($this->someInstance, 'someMethod')
这意味着您要么使 getScore()
静态化,要么创建 City
的实例并使用它(也许使用 依赖注入 来获取它)。
你将不得不做这样的事情,使用被过滤的实际变量。在这里,我猜 cities
是一个集合:
class ScoreExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
$filter = new Twig_SimpleFilter('score', array($this, 'getCityScore'))
);
}
public function getCityScore($cities)
{
$scores = array();
foreach ($cities as $city) {
$scores[] = $city->getScore();
}
return $scores;
}
public function getName()
{
return 'score_extension';
}
}