2013-05-30 106 views
1

我正在創建一個簡單的網站,用戶可以在其中註冊,然後登錄並添加文本文章。如果沒有登錄,訪問者將扮演訪客的角色,並且只能查看文章。我正在做Zend框架1的練習,因爲我剛開始學習Zend。我將爲控制器AuthController進行登錄,但我想知道如何重定向到該控制器中的登錄操作,從我在IndexController中的indexAction中進行操作。另外,如何使用自定義插件來實現這種訪問控制?我如何調用它?如何重定向到Zend中的其他控制器操作

回答

10

可以使用的操作方法內重定向:

$this->redirect('/module/controller/action/'); 
+1

我想這在最新版本的ZF中被重命名爲$ this-> redirect()。 – wmac

0

我認爲這將有助於你

$this->_helper->redirector('controller','action'); 

通過這種方式,您可以調用另一個控制器。

14

jalpesh和Hasina對他們的簡短答案都是正確的。

Jalpesh的示例將是動作助手redirector()的速記呼叫,默認爲gotoSimple()重定向方法,除非他似乎具有倒退參數。

//corrected 
$this->_helper->redirector($action, $controller); 

會比較冗長稱爲:

$this->getHelper('Redirector')->gotoSimple($action, $controller = null, $module = null, array $params = array()); 

有幾種方式使用Redirector action helper,這只是一個很普通的例子。

由Hasina提供的例子是一個調用控制器utility method _redirect()一個更有限的代碼位然後Redirector幫手仍然非常有用。

//only accepts a url string as the first arg 
//deprecated as of ZF 1.7 still in documentation 
$this->_redirect($url, array $options = array()); 

顯然是ZF 1.7的有一個新的方法的文檔不是在(發現在文檔塊這個位)是首選:

//valid as of ZF 1.7, not in documentation 
$this->redirect($url, array $options = array()); 

這兩種工具方法是代理爲:

Zend_Controller_Action_Helper_Redirector::gotoUrl() 

希望這有助於

+0

謝謝RockyFord!使用$ this - > _ redirect('/ auth/login')我得到這個錯誤:在這個服務器上找不到請求的URL/demo/auth/login。我的項目文件夾是演示,其中我有文件夾應用程序,庫和公共。 index.php在public中,我將瀏覽器指向localhost/demo/public /。 –

2

如果你正在使用路由,你可以使用$this->getHelper('Redirector')->setGotoRoute(array(), 'routeName');

相關問題