我需要在我的 Zend Framework 应用程序中创建一些自定义表单元素(带有自定义 View 助手)。问题是他们每个人都如此相似。我想创建一个基本 View 帮助器类,每个类都可以扩展它,并实现我需要的抽象函数。
解决方案:
因此,如果我的 Picker
元素是抽象类,而 ContactPicker
和 OrganizationPicker
是扩展类...
表单元素:
class My_Form_Element_ContactPicker extends My_Form_Element_Picker
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = "contactPickerElement";
}
View 助手:
class My_View_Helper_ContactPickerElement extends My_View_Helper_PickerElement
{
public function contactPickerElement($name, $value = null, $attribs = null)
{
// I don't need to do anything in this function.
// I only need the parent to do all the work.
return parent::pickerElement($name, $value, $attribs);
}
protected function myAbstractFunctionThatMustBeImplemented()
{
// This function will do all the work specific to this extending class.
$model = new ContactModel();
return $model->foobar;
}
}
这是抽象 View 助手:
abstract class Evanta_View_Helper_PickerElement extends Zend_View_Helper_FormElement
{
/**
* This function would have been called automatically, but since it's being extended...
* Any extending classes must remember to manually call this function
*/
public function modalPickerElement($name, $value = null, $attribs = null)
{
$html = 'My picker element HTML';
return $html;
}
/**
* This function must be implemented by any extending classes
*/
abstract protected function myAbstractFunctionThatMustBeImplemented();
}
最佳答案
在 Bootstrap
中的某个位置,您需要定义表单资源的去向,我建议使用自动加载器:
$autoloader->addResourceType('default_form', 'forms/', 'Form');
我加载自动加载器的整个方法如下所示
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default_',
'basePath' => $this->_root,
));
// Here a define a resource named 'default_form', this can be anything,
// 2nd param is the path relative to my application folder,
// and 3rd param is the prefix for the classes inside that folder
$autoloader->addResourceType('default_form', 'forms/', 'Form');
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
return $autoloader;
}
然后,您可以在此 forms
文件夹中创建一个名为 Element
的文件夹,然后创建一个名为 Default_Form_Element_Picker.php
的文件并定义该类,您可以可以重写诸如 isValid()
、setValues()
、getValue()
、loadDefaultDecorators()
和 __construct()
Default_Form_Element_Picker extends Zend_Form_Element_Xhtml {
}
您可以通过创建名为 Default_Form_Element_ContactPicker.php
和 Default_Form_Element_OrganizationPicker.php
的文件来扩展此类以创建其他表单元素
Default_Form_Element_ContactPicker extends Default_Form_Element_Picker {
}
Default_Form_Element_OrganizationPicker extends Default_Form_Element_Picker {
}
您还可以在此处添加一个用于创建表单的类,将其命名为 Default_Form_A.php
Default_Form_A extends Zend_Form {
public function __construct()
{
// create form elements here suing Zend_Form_Element and your new Custom Elements
}
}
文件结构
application
|--forms
|----Default_Form_A.php
|----Element
|------ Default_Form_Element_Picker.php
|------ Default_Form_Element_ContactPicker.php
|------ Default_Form_Element_OrganizationPicker.php
关于php - Zend 框架 : How to create an abstract view helper for other view helpers to extend?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3765408/