在Symfony 4中,我的自定义树枝扩展有一个注册问题。我创建了帮助我解码json数据的扩展程序,但这不是工作。当我想使用我的json_decode过滤器时,将显示此消息。错误消息
我的自定义细枝过滤器的代码:
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getName() {
return 'Json Decode';
}
public function getFunctions()
{
return [
new TwigFilter('json_decode', [$this, 'json_decode']),
];
}
public function json_decode($input, $assoc = false) {
return json_decode($input,$assoc);
}
}
?>这是一个twig_exension.yaml
services:
_defaults:
public: false
autowire: true
autoconfigure: true
# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: null
#Twig\Extensions\DateExtension: null
Twig\Extensions\IntlExtension: null
Twig\Extensions\TextExtension: null
App\Twig\AppExtension: null这是在我的小枝文件中返回和错误的一行
{% set commande = render(controller('App\\Controller\\StoreController::getProduitsCommandes')) | json_decode %}下面是StoreController.php中的响应返回
$response = new Response(json_encode(["produits"=>$produitsArray,"total_ht"=>$total_ht,"tva"=>$tva,"nbre_produits"=>$nbre_produits]));
$response->headers->set('Content-Type', 'application/json');
return $response;当我输入php /控制台调试:twig-filter=json_decode时,调试器将返回这个结果。
---------
* json_decode(input, assoc = false)谢谢你的关注,如果有人有办法的话,它会帮助我的
发布于 2019-12-05 06:59:34
由于错误声明,无法找到filter。这是由于您试图将您的filter注册为一个函数,将注册移到getFilters方法中。同时,将现有函数链接起来也是完全可行的。
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('json_decode', 'json_decode'), //just chain to existing PHP function
];
}
}sidenote方法getName现在已经过时,可以删除它,因为它已经被弃用,并且不再在代码中使用
https://stackoverflow.com/questions/59178557
复制相似问题