2012-09-22 31 views
0

我有這樣一個網址:view_test.php?user=56如何使用Zend Route Regex將URL路由到另一個?

,我想在這裏得到它:

$router->addRoute(
     'test', 
     new Zend_Controller_Router_Route(
      'test/view/:id', 
      array(
       'module' => 'test', 
       'controller' => 'index', 
       'action' => 'view' 
      ) 
     ) 
    ); 

基本上是從view_test.php?user=56/test/view/56

林不知道如何處理?

有什麼想法?

感謝

回答

0

據我所知,在路線,你不能引用網址的查詢字符串的內容,只是它的請求路徑。但是你可以做到以下幾點:

添加了舊的URL映射到一個重定向操作路線:

$router->addRoute('view-test-redirect', new Zend_Controller_Router_Route_Static(
    'view_test.php', 
    array(
     'module' => 'test', 
     'controller' => 'index', 
     'action' => 'view-test-redirect', 
    ) 
); 

此外,添加路由代表你的「真實」的行動:

$router->addRoute('view-test', new Zend_Controller_Router_Route(
    'view/test/:user', 
    array(
     'module' => 'test', 
     'controller' => 'index', 
     'action' => 'view-test', 
    ) 
); 

動作名稱view-test-redirect對應於以下操作:

public function viewTestRedirectAction() 
{ 
    $user = (int) $this->_getParam('user', null); 
    if (!$user){ 
     throw new \Exception('Missing user'); 
    } 
    $this->_helper->redirector->goToRouteAndExit(array('user' => $user), 'view-test'); 
} 

然後您的view-test動作可以以預期的方式做的事情:

public function viewTestController() 
{ 
    $user = (int) $this->_getParam('user', null); 
    if (!$user){ 
     throw new \Exception('Missing user'); 
    } 
    // Read db, assign results to view, praise unicorns, etc. 
} 

沒有測試,只是大腦傾銷驗證這個想法。

相關問題