请帮助解决以下错误。我正在关注Zend Framework documentation。
致命错误:
Cannot use 'Int' as class name as it is reserved in E:\Working\PHP\Zend-Framework\Zend-Framework-2\zf2-stable\vendor\zendframework\zendframework\library\Zend\Filter\Int.php on line 12
错误:
array('name' => 'Int'), ## Fatal error
array('name' => 'ToInt') ## Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for Zend\Filter\ToInt
array('name' => 'StringTrim') ## Works Only for Edit and Delete, however Not for Add
环境:
$ php --version
PHP 7.0.9 (cli) (built: Jul 20 2016 11:08:23) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
$ composer --version
Composer version 1.2.1 2016-09-12 11:27:19
$ composer show
zendframework/zendframework 2.3.3 Zend Framework 2
zendframework/zendxml 1.0.2 Utility library for XML usage, best practices, and security in PHP
ZF2教程/模块/专辑/ SRC /专辑/型号/ Album.php
<?php
namespace Album\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Album implements InputFilterAwareInterface
{
public $id;
public $artist;
public $title;
protected $inputFilter;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'), ## Fatal error
),
));
$inputFilter->add(array(
'name' => 'artist',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
ZF2教程/模块/专辑/视图/专辑/专辑/ add.phtml
<?php
// module/Album/view/album/album/add.phtml:
$title = 'Add new album';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
/*
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
*/
感谢。
答案 0 :(得分:1)
我很确定你应该在这个版本中使用IsInt过滤器而不是Int过滤器。
Zend\I18n\Validator\IsInt
答案 1 :(得分:0)
我使用"zendframework/zendframework": "2.3.9",
1)创建类 ToInt.php
<?php
namespace Application\Filter;
use Zend\Filter\AbstractFilter;
class ToInt extends AbstractFilter
{
/**
* Returns (int) $value
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @param string $value
* @return ToInt|mixed
*/
public function filter($value)
{
if (!is_scalar($value)) {
return $value;
}
$value = (string) $value;
return (int) $value;
}
}
2)输入
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => '\Application\Filter\ToInt'),
),
)));