2011-05-23 74 views
3

在Zend Framework中,我有一個Action Helper,它可以在大多數頁面上加載登錄表單。這發生在Helper的preDispatch()方法中,我想在表單上設置setAction(),以便它回發到當前的URL。Zend,如何從Action Helper中訪問當前路由

從Action Helper中訪問當前URL /路由的最佳方式是什麼?訪問請求(通過操作控制器),然後拉動getActionName()和getControllerName(),並將它們與baseURL()連接起來?

有沒有更簡單的方法? (設置操作需要URI字符串作爲參數)。

謝謝!

回答

2

我發現我並不需要從動作助手內訪問當前URL /路徑。通過將表單動作留空,它會自動發佈到當前URL。完善。

+0

實際上它是標準形式的行爲。如果您需要當前網址,則可以使用不帶參數的網址瀏覽幫助程序 – Xerkus 2011-05-23 07:15:27

0

如果我理解正確,當用戶登錄時,您想要將它們發送回它們來自的頁面。我用這樣做的代碼是:

// the user has come from a particular page - send them back 
if($_SERVER['HTTP_REFERER']) { 
    $this->_redirect($_SERVER['HTTP_REFERER']); 
} else { 
// the user has come from the home page, or this page 
    $this->_redirect('/'); 
} 

它位於登錄操作(即LoginController->loginAction())。

5

你可以像@Elie建議的那樣做。但是,如果你想用這個方法ZF,你可以看看這個:

$request = Zend_Controller_Front::getInstance()->getRequest(); 
    echo $request->getHeader('referer'); // referer's address 
    echo $request->getRequestUri();  // current address