【发布时间】:2014-08-02 19:37:40
【问题描述】:
我正在尝试为我的项目创建一个参数转换器 (symfony 2.4) 这是我的转换器:
namespace Test\ParamConvertersBundle\ProgramConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
class ProgramConverter implements ParamConverterInterface{
protected $class;
protected $repository;
public function __construct($class, EntityManager $em){
$this->class = $class;
$this->repository = $em->getRepository($class);
}
public function apply(Request $request, ParamConverter $configuration){
return true;
}
public function supports(ParamConverter $configuration){
return $this->class === $configuration->getClass();
}
}
还有一个让我抓狂的例外:
FatalErrorException: 编译错误:Test\ParamConvertersBundle\ProgramConverter\ProgramConverter::apply() 的声明必须与 Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface::apply(Symfony\Component\HttpFoundation\Request $request 兼容, Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter $configuration)
我不明白这个问题......
【问题讨论】:
-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;? ;) -
我认为这行不通。实际上他需要:
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Sensio然后在方法签名中使用Sensio\ParamConverter;) -
工作就像一个魅力,谢谢 bartek :)