Zend Link Helper существует? - PullRequest
       1

Zend Link Helper существует?

3 голосов
/ 09 октября 2010

В некоторых других фреймворках есть помощник по ссылкам, например output_link('anchor', 'destination');, для замены необходимости вводить <a href=""></a> в шаблон.Есть ли у Zend нечто подобное?и нужно ли сначала объявить ссылку в действии, прежде чем я смогу использовать ее в средстве просмотра?

Ответы [ 5 ]

5 голосов
/ 09 октября 2010

Zend_View_Helper_Url может генерировать URL в поле зрения, посмотрите его документацию по API http://framework.zend.com/apidoc/core/Zend_View/Helper/Zend_View_Helper_Url.html

2 голосов
/ 09 октября 2010

Я не уверен, есть ли у Zend это, но все, что вам нужно сделать, это создать свой outputLink в View Helper (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>
1 голос
/ 14 октября 2011

Вот мой помощник вида элемента привязки для 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
0 голосов
/ 24 ноября 2011

Что ж, хелпер Зендов вроде как сосет.Это единственное, что меня мучает при разработке приложений на Zend.В Codeigniter url helper приходил очень кстати.Zend имеет очень ограниченные ресурсы в случае этого.Мне пришлось портировать URL помощника CI для использования в моих Zend Apps.Более того, у Symfony не так много вспомогательных методов, как у CI, и я не знаю почему.

0 голосов
/ 09 октября 2010

Нет, вы должны сделать один.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...