Zend框架:元属性集成

Fra*_*ank 6 facebook zend-framework metadata

我正在尝试根据页面内容将一些元素(以下格式)添加到我的页面的头部:

<meta property="og:title" content="some content" />
Run Code Online (Sandbox Code Playgroud)

使用headMeta() - > appendName如下:

$this->view->headMeta()->appendName('og:title', 'some content');
Run Code Online (Sandbox Code Playgroud)

在标头中生成以下内容:

<meta name="og:title" content="some content" />
Run Code Online (Sandbox Code Playgroud)

有没有办法让Zend 使用属性字段生成元素

谢谢

Dav*_*aub 8

听起来你需要创建自己的视图助手,扩展标准的Zend Framework HeadMeta视图助手,并实现一个名为的appendProperty()模拟方法appendName().

由于该appendName()方法似乎是在__call()方法中处理的,看起来您的扩展类可以简单地复制__call()父类的相同形式,但是更改preg_match()from中使用的模式:

'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/'

'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/'

[作为旁注,可能值得向ZF跟踪器提出问题,建议将此正则表达式模式从内联代码中拉出,然后将其放置为类的受保护成员.这样,一个子类 - 就像你的一样 - 可以简单地声明一个新的模式,而不是"复制"这么多的父代码.但在我向他们提出建议之前,我必须先查看并测试一下.]

无论如何,只是在黑暗中刺伤......

更新:2010-12-17

我发现需要更多才能使它工作.您需要覆盖受保护的成员$_typeKeys和受保护的方法_normalizeType()来处理新的"属性"类型.

您的扩展类看起来像这样:

class Kwis_View_Helper_HeadMeta extends Zend_View_Helper_HeadMeta
{
    protected $_typeKeys     = array('name', 'http-equiv', 'charset', 'property');

    public function __call($method, $args)
    {
        if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
            $action = $matches['action'];
            $type   = $this->_normalizeType($matches['type']);
            $argc   = count($args);
            $index  = null;

            if ('offsetSet' == $action) {
                if (0 < $argc) {
                    $index = array_shift($args);
                    --$argc;
                }
            }

            if (2 > $argc) {
                require_once 'Zend/View/Exception.php';
                $e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
                $e->setView($this->view);
                throw $e;
            }

            if (3 > $argc) {
                $args[] = array();
            }

            $item  = $this->createData($type, $args[0], $args[1], $args[2]);

            if ('offsetSet' == $action) {
                return $this->offsetSet($index, $item);
            }

            $this->$action($item);
            return $this;
        }

        return parent::__call($method, $args);
    }

    protected function _normalizeType($type)
    {
        switch ($type) {
            case 'Property':
                return 'property';
            default:
                return parent::_normalizeType($type);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如前所述,如果preg_match()Zend_View_Helper_HeadMeta::__call()入的模式被分解为一个名为的受保护成员,则可能会短得多$_callPattern.然后扩展类不必复制__call()方法的大部分.它只会有覆盖保护成员$_typeKeys$_callPattern贯彻保护的方法_normalizeType(),如上图所示.