[英]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.