Zend链接助手存在吗?

时间:2010-10-08 20:27:59

标签: php zend-framework

其他一些框架有一个像output_link('anchor', 'destination');这样的链接帮助器,以取代在模板中键入<a href=""></a>的需要。 Zend有类似的东西吗?在我可以在查看器中使用它之前,我是否必须首先声明操作中的链接?

5 个答案:

答案 0 :(得分:5)

Zend_View_Helper_Url可以在视图中生成URL,看看它的API文档 http://framework.zend.com/apidoc/core/Zend_View/Helper/Zend_View_Helper_Url.html

答案 1 :(得分:2)

我不确定Zend是否有此功能,但您需要做的只是在View HelperoutputLink)中创建自己的applications/views/helpers/并根据需要设置,应该是非常微不足道的。

class Zend_View_Helper_OutputLink extends Zend_View_Helper_Abstract
{
    public function outputLink($anchor, $description)
    {
        return '<a href="' . $anchor . '">' . $description . '</a>';
    }
}

只需按照您想要的方式进行修改即可。您可以在下面的视图中调用它:

<span><?php $this->outputLink('test.html', 'Test Me!'); ?> </span>

答案 2 :(得分:1)

这是我的zend的锚元素视图助手。您需要使用我的图像元素视图助手或删除使用它的代码部分,以防您不喜欢它。当然,您可以自由修改名称和其他任何您想要的内容。     

require_once 'Zend/View/Helper/HtmlElement.php';

class Ecoweb_View_Helper_AnchorElement extends Zend_View_Helper_HtmlElement {

    /**
     *
     * @param string $url
     * @param string $content
     * @param array|string $attribs
     * @return string 
     */
    public function anchorElement($url, $content = '', $attribs = null)
    {
        if (is_array($url)) {
            $reset = isset($url[2]) ? $url[2] : false;
            $encode = isset($url[3]) ? $url[3] : false;
            $url = $this->view->url($url[0], $url[1], $reset, $encode);
        } else {
            $url = $this->view->baseUrl($url);
        }

        if (is_array($attribs)) {
            $attribs = $this->_htmlAttribs($attribs);
        } else {
            $attribs = empty($attribs) ? '' : ' '.$attribs;
        }

        if (is_array($content) && isset($content['src'])) {
            $src = $content['src'];
            $alt = isset($content['alt']) ? $content['alt'] : null;
            $imgAttribs = isset($content['attribs']) ? $content['attribs'] : array();

            $content = $this->view->imgElement($src, $alt, $imgAttribs);
        }
        $content = empty($content) ? $url : $this->view->escape($content);

        $xhtml = '<a '
                . 'href="'.$url.'"'
                . $attribs
                . '>'
                . $content
                . '</a>';

        return $xhtml;
    }

}

这是图像元素视图助手:

<?php

require_once 'Zend/View/Helper/HtmlElement.php';

class Ecoweb_View_Helper_ImgElement extends Zend_View_Helper_HtmlElement {

    /**
     *
     * @param string $src
     * @param string $alt
     * @param array|string $attribs
     * @return string 
     */
    public function imgElement($src, $alt = '', $attribs = null)
    {
        $src = $this->view->baseUrl($src);

        if (is_array($attribs)) {
            $attribs = $this->_htmlAttribs($attribs);
        } else {
            $attribs = empty($attribs) ? '' : ' '.$attribs;
        }

        $alt = $this->view->escape($alt);

        $xhtml = '<img '
                . 'src="'.$src.'" '
                . 'alt="'.$alt.'"'
                . $attribs
                . $this->getClosingBracket();

        return $xhtml;
    }

}

用例:

echo $this->anchor('/mycontroller/myaction');
// output: <a href="/mycontroller/myaction">/mycontroller/myaction</a>

echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: <a href="/mycontroller/myaction" rel="nofollow">My anchor content</a>

echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: <a href="http://mydomain.com/mycontroller/myaction" rel="nofollow">My anchor content</a>
// when baseUrl is http://mydomain.com

echo $this->anchor(array(array('controller' => 'mycontroller', 'action' => 'myaction'), 'myroute'), 'My anchor content', array('rel' => 'nofollow'));
// output: <a href="/mycontroller/myaction" rel="nofollow">My anchor content</a>

echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png'));
// output: <a href="/mycontroller/myaction"><img src="/uploads/myimag.png" alt=""></a>
// when you have an html doctype

echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png', 'alt'=>'My alt text', array('width' => '100')));
// output: <a href="/mycontroller/myaction"><img src="/uploads/myimag.png" alt="My alt text" width="100" /></a>
// when you have an xhtml doctype

答案 3 :(得分:0)

好吧,Zend的url helper类似的东西有点糟糕。这是在zend中开发应用程序时唯一让我感到痛苦的事情。在Codeigniter中,url helper过去非常方便。 Zend的资源非常有限。我不得不移植CI的url助手以在我的Zend Apps中使用。而且,Symfony没有像CI这样的许多辅助方法,我不知道为什么。

答案 4 :(得分:-1)

不,你必须制作一个。