[英]How to add a trailing slash on URL
我希望我网站上的所有URL的末尾都有斜杠。 我在Zend中为URLHelper创建了一个简单的扩展。 现在,它将所有单词分隔符更改为连字符(-)。 添加功能以添加尾部斜杠会很棒。 评论出来的行(hack)不起作用。 斜杠最终被URL编码。
我知道这必须很容易解决,而且要摆在我的面前,但它在逃避我。 :\\
class Ace_Helpers_Url extends Zend_View_Helper_Url
{
/**
* Generates an url given the name of a route.
*
* @access public
*
* @param array $urlOptions Options passed to the assemble method of the Route object.
* @param mixed $name The name of a Route to use. If null it will use the current Route
* @param bool $reset Whether or not to reset the route defaults with those provided
* @return string Url for the link href attribute.
*/
public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = false)
{
if (is_array($urlOptions)) {
foreach ($urlOptions as $index => $option) {
$urlOptions[$index] = trim(strtolower(str_replace(' ', '-', $option)));
#$urlOptions[$index] .= '/'; #Add trailing slash for continuity
}
}
$router = Zend_Controller_Front::getInstance()->getRouter();
return $router->assemble($urlOptions, $name, $reset, $encode);
}
}
手动添加它并不难:
<?php echo $this->url(array(
'controller' => 'index'
)).'/'; ?>
如果要避免url编码,请查看url helper的第四个参数: encode
。 将其设置为false,并且不会对您的输入进行url编码,因此您可以执行以下操作:
<?php echo $this->url(array(
'alias' => 'blog/2011/09/example-blog-entry'
),'alias',true,false); ?>
我的解决方案基于此讨论 。
应用/视图/助手/ Url2.php
class Zend_View_Helper_Url2 extends Zend_View_Helper_Url
{
/**
* Keeps Url()'s original params and their default values, so we don't have to
* learn yet another method.
*/
public function url2(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
{
return parent::url($urlOptions, $name, $reset, $encode) . '/';
}
}
在某些控制器中
echo $this->view->url2(array('controller' => 'index'));
或某些视图
echo $this->url2(array('controller' => 'index'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.