2011-09-20 99 views
0

我想在我的Zend Framework應用程序來創建搜索引擎友好的網址,但搜索引擎友好的URL怎麼是這個正確的語法:使用Zend路由器

​​

:ID_:標題顯然不能,因爲Zend公司沒工作不知道_是分隔符?我需要使用正則表達式路由器嗎?還是使用普通路由器?

回答

2

事實上,一個正則表達式路線可以做到這一點。

如果你不想使用正則表達式路線的任何原因,有通過前端控制器插件一個簡單的解決方法:

//replace the :id and :title params with a single one, mapping them both 
$newsroute = new Zend_Controller_Router_Route(
     'news/:action/:article', 
     array('controller' => 'news') 
    ); 

// in a front controller plugin, you extract the Id form the article param 
function function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { 

    if($request->getParam('article', false)){ 

     $slug = $request->getParam('article'); 
     $parts = array(); 
     preg_match('/^(\d+)/', $slug, $parts); 

     // add the extracted id to the request as if there where an :id param 
     $request->setParam('id', $parts[0]); 
    } 
} 

當然,你也可以提取標題以同樣的方式,如果你需要它。

$this->url(array('article' => $id.'_'.$title)); 
2

爲了避免與鏈接處理包含特殊字符,你可以使用這個插件爲Zend框架:

當你想生成URL不要忘記建立自己的「文章」 PARAM。

https://github.com/btlagutoli/CharConvert

$filter2 = new Zag_Filter_CharConvert(array(
       'onlyAlnum' => true, 
       'replaceWhiteSpace' => '-' 
      )); 
echo $filter2->filter('éééé ááááá ? 90 :');//eeee-aaaaa-90 

這可以幫助您應對其他語言的字符串

+0

正確鏈接:https://github.com/agutoli/CharConvert – JellyBelly