Php Zend链接帮助器是否存在?
其他一些框架有一个链接助手,比如Php Zend链接帮助器是否存在?,php,zend-framework,Php,Zend Framework,其他一些框架有一个链接助手,比如output_link('anchor','destination')替换在模板中键入的需要。Zend也有类似的情况吗?在查看器中使用链接之前,是否必须先在操作中声明链接 我不确定Zend是否有此功能,但您所需要做的只是在(应用程序/视图/帮助程序/)中创建自己的输出链接,并按照您的意愿进行设置,这应该很简单 class Zend_View_Helper_OutputLink extends Zend_View_Helper_Abstract { publ
output_link('anchor','destination')
替换在模板中键入
的需要。Zend也有类似的情况吗?在查看器中使用链接之前,是否必须先在操作中声明链接 我不确定Zend是否有此功能,但您所需要做的只是在(应用程序/视图/帮助程序/
)中创建自己的输出链接
,并按照您的意愿进行设置,这应该很简单
class Zend_View_Helper_OutputLink extends Zend_View_Helper_Abstract
{
public function outputLink($anchor, $description)
{
return '<a href="' . $anchor . '">' . $description . '</a>';
}
}
class Zend\u View\u Helper\u OutputLink扩展了Zend\u View\u Helper\u抽象
{
公共函数outputLink($anchor,$description)
{
返回“”;
}
}
只要按你想的方式修改就行了。在你看来,你可以这样称呼它:
<span><?php $this->outputLink('test.html', 'Test Me!'); ?> </span>
Zend\u View\u Helper\u Url可以在视图中生成Url,请查看其API文档
不,您必须制作一个。这是我的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;
}
}
require_once'Zend/View/Helper/HtmlElement.php';
类Ecoweb\u View\u Helper\u AnchoreElement扩展了Zend\u View\u Helper\u HtmleElement{
/**
*
*@param string$url
*@param string$content
*@param数组|字符串$attribs
*@返回字符串
*/
公共函数anchoreElement($url、$content='',$attribs=null)
{
if(is_数组($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);
}否则{
$url=$this->view->baseUrl($url);
}
if(is_数组($attribs)){
$attribs=$this->htmlAttribs($attribs);
}否则{
$attribs=空($attribs)?“”:'.$attribs;
}
if(is_数组($content)和&isset($content['src'])){
$src=$content['src'];
$alt=isset($content['alt'])?$content['alt']为空;
$imgAttribs=isset($content['attribs'])?$content['attribs']:数组();
$content=$this->view->imgElement($src、$alt、$imgAttribs);
}
$content=空($content)?$url:$this->view->escape($content);
$xhtml='';
返回$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;
}
}
嗯,Zend的url助手有点差劲。这是我在zend中开发应用程序时唯一感到痛苦的事情。在Codeigniter中,url帮助器曾经非常方便。在这种情况下,Zend的资源非常有限。我必须移植CI的url帮助程序才能在我的Zend应用程序中使用。而且,Symfony没有像CI那样的助手方法,我不知道为什么 对于输出HTML标记,最好扩展
Zend\u View\u Helper\u HtmlElement
而不是Zend\u View\u Helper\u Abstract
。它有很好的处理HTML属性的方法。我将对此进行研究。谢谢
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