Is there a link to the Zend link?

Some other frames have an auxiliary communication element, for example output_link('anchor', 'destination');, to replace the need for input <a href=""></a>in the template. Does Zend have something similar? and do I need to declare the link in action first before I can use it in the viewer?

+3
source share
5 answers

Zend_View_Helper_Url can generate the URL in the view, view its API document http://framework.zend.com/apidoc/core/Zend_View/Helper/Zend_View_Helper_Url.html

+5
source

, 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>
+2

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
+1

Well, the assistant assistant to Zend seems to suck. This is the only thing that hurts during the development of applications in zend. In Codeigniter, the URL helper was very convenient. In this case, Zend has very limited resources. I had to plug in the CI URL Assistant for use in my Zend applications. And besides, Symfony doesn't have support methods like CI, and I'm not sure why.

0
source

No, you have to do it.

-1
source

Source: https://habr.com/ru/post/1768693/


All Articles