我正在将继承的Zend Framework 2应用程序迁移到Zend Framework 3,并且在注册我的自定义表单视图助手时遇到了一些困难。当应用程序使用版本2时,帮助程序工作,主要用于为可访问性添加标记属性。例如,这是一个自定义的FormText.php助手。
<?php
namespace Application\Form\View\Helper;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormInput;
class FormText extends FormInput
{
/**
* Attributes valid for the input tag type="text"
*
* @var array
*/
protected $validTagAttributes = array(
'name' => true,
'autocomplete' => true,
'autofocus' => true,
'dirname' => true,
'disabled' => true,
'form' => true,
'list' => true,
'maxlength' => true,
'pattern' => true,
'placeholder' => true,
'readonly' => true,
'required' => true,
'size' => true,
'type' => true,
'value' => true,
'aria-hidden' => true,
'aria-invalid' => true,
'aria-describedby' => true,
'aria-label' => true,
);
/**
* Determine input type to use
*
* @param ElementInterface $element
* @return string
*/
protected function getType(ElementInterface $element)
{
return 'text';
}
}
在我的应用程序的第2版中,帮助程序使用以下方法在Module.php中注册(不确定为什么不在module.config.php中)(为简洁起见,仅显示1个帮助程序):
public function getViewHelperConfig()
{
return array(
'invokables' => array(
// Form helpers
'FormText' => 'Application\Form\View\Helper\FormText',
),
);
}
在应用程序的ZF3版本中,我试图在module.config.php的return语句中使用以下数组元素:
'view_helpers' => [
'factories' => [
View\Helper\Cdn::class => View\Helper\CdnFactory::class,
Form\View\Helper\FormText::class => InvokableFactory::class,
],
'aliases' => [
'cdn' => View\Helper\Cdn::class,
'FormText' => Form\View\Helper\FormText::class,
],
],
这对于表单视图助手不起作用,尽管'cdn'助手正在正确注册并且正常工作。表单视图助手不需要任何注入依赖项,因此我没有使用自定义工厂类。
我确实将'Zend / Form'列为application.config.php中的模块,并且知道标准的Zend表单视图助手正在工作。
我没有成功尝试上面代码的许多变体来使用来自SO问题的代码示例来注册帮助程序,尽管所有问题似乎都与普通视图助手相关而不是表单视图助手。
我将非常感谢有关如何使其发挥作用的任何建议。
谢谢。
以我工作的公司的一个活跃项目为例。我们还有一些默认的ZF3 Form ViewHelpers,用我们自己的方式覆盖前端框架。主题名称是“Alpha”(我认为;-))
我们使用以下内容:
'view_helpers' => [
// other stuff
'invokables' => [
'Zend\Form\View\Helper\FormCollection' => AlphaCollection::class,
'Zend\Form\View\Helper\Form' => AlphaForm::class,
'Zend\Form\View\Helper\FormRow' => AlphaRow::class,
'Zend\Form\View\Helper\FormSelect' => AlphaSelect::class,
],
],
查看助手本身:
// Namespace + use statements
class AlphaCollection extends FormCollection
{
public function __construct()
{
parent::setWrapper('<div class="alpha-form-collection">%2$s%1$s%3$s</div>');
}
/**
* @param \Zend\Form\ElementInterface $element
* @param null $labelPosition
* @return string
*/
public function render(ElementInterface $element, $labelPosition = null)
{
$markup = parent::render($element, $labelPosition);
$classes = 'input-field col s12 alpha-fieldset';
if($element instanceof Collection)
{
$classes .= ' alpha-fieldset-collection';
}
$prepend = '<div class="' . $classes . '">';
$append = '</div>';
return $prepend . $markup . $append;
}
}
所以从本质上讲,我们并不是在创建自己的ViewHelpers,而是更改Zend Framework 3提供的那些。因为我们只是“更新”现有的,所以我们不需要创建新的工厂(没有额外的要求) )。
Zend Framework已经注册了具有可调用名称的ViewHelpers(所以你可以做$this->formRow(...)
或$this->formSelect(...)
。我们刚刚劫持了他们的配置并用我们自己的类替换了我们需要的类。这样,当我们完全生成一个表单(<?= $this->form($form) ?>
)时,ZF会我们所有的工作。
在.phtml
实施:
<!-- Internally uses the invokables we've modified, so this is all we need to do :) -->
<?= $this->form($form) ?>
为了使配置更具前瞻性,我认为你现在可以用FQCN替换可调用的字符串(还没有测试过这个(还))