2012-01-28 27 views
0

PHP的,我有方法添加路由:Zend的路線正則表達式和無效的控制器



    public function addRoutes() 
      { 
       $front = Zend_Controller_Front::getInstance(); 
       $redirect = $front->getRouter(); 
       $router = new Zend_Controller_Router_Route_Regex( 
                  "p\/(a-zA-Z0-9)\.htm", 
                 array(
                 'controller'=>'page', 
                 'action'=>'index', 
                  1=>'ja.htm' 
                 ), 
                 array(1 => 'page_name') 
         ); 
       $route2 = new Zend_Controller_Router_Route_Regex("(a-zA-Z0-9)\.html", 
         array('controller'=>'page', 
          'action'=>'index', 
          1=>'ja.html'), 
         array(1=>'page_name')); 
       $redirect->addRoute('pages',$router); 
       $redirect->addRoute('hmtmled',$route2); 
       $front->setRouter($redirect); 


      } 


我試着輸入網址,如:P/ja.htm,但我得到的錯誤:無效的控制器指定(P)。我知道它的默認路由的原因,但如何改變呢?

+0

我不認爲你需要逃跑的斜線,所以它的工作原理,如果你正則表達式就是'p /(A-ZA-Z0-9)\。htm'? – 2012-01-28 22:28:33

+0

你的模式在'p /'之後只接受一個_single_ alphanum char。 (注意附加'+'限定符):'p /(a-zA-Z0-9)+ \。htm' – 2012-01-29 03:15:36

+0

即使我的模式是p /(a-zA-Z0-9 )+ \。htm我得到相同的錯誤,即使有選項像p /(\ w + \ d +)\。htm仍然失敗,所以我不認爲它的正則表達式錯誤 – Axxxon 2012-01-29 06:37:41

回答

0

是您Bootstrap類的那個方法的一部分?如果是這樣,你確定它正在運行?請記住,自動調用的Bootstrap方法的格式爲_initXXX()(注意前導下劃線)。

而且,@Tim噴泉在評論中指出精明,正則表達式必須是:

p/([0-9A-Za-z]+)\.htm 
0

您嘗試刪除默認的路由:

//excerpt from ZF reference 24.5.4. Default Routes... If you do not want this particular default route in your routing schema, you may override it by creating your own 'default' route (i.e., storing it under the name of 'default') or removing it altogether by using removeDefaultRoutes():

// Remove any default routes 
$router->removeDefaultRoutes();