2010-03-12 123 views
1
<?php 
class PI_Controller_Plugin_AssetGrabber extends Zend_Controller_Plugin_Abstract 
{ 
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) 
{ 
    /* 
     The module name 
    */ 
    $moduleName = $request->getModuleName(); 
    /* 
     This modules requires the user to be loggedin in order to see the web pages! 
    */ 
    $loginRequiredModules = array('admin'); 

    if (in_array($moduleName,$loginRequiredModules)) { 
     $adminLogin = new Zend_Session_Namespace('adminLogin'); 
     if (!isset($adminLogin->loggedin)) { 
      /*-------------------------------------- 
       Here I want to redirect the user 
      */ 
      $this->_redirect('/something'); 
     } 
    } 
} 
} 

我正在嘗試做一個重定向$this->_redirect('/something')但不起作用!你知道我在這種情況下怎麼做重定向?Zend Framework url重定向

最好的問候,

+0

什麼是錯誤消息剩餘?沒有?簡單地不重定向?你也可以嘗試$ this - > _ helper-> redirector(),或者$ this - > _ helper-> gotoUrl()在http://framework.zend.com/manual/en/zend.controller.actionhelpers.html上看到更多內容。 #zend.controller.actionhelper.redirector.basicusage。 – 2010-03-12 14:09:56

+0

我不能在我的插件中使用這些東西,因爲只能使用'$ this - > _ redirect' ..只有當你擴展Zend_Controller_Action – Uffo 2010-03-12 14:56:30

回答

2

可以使用Zend_Controller_Action_HelperBroker來獲取重定向幫助程序或直接從Request對象執行重定向。

見例子

+0

也許我會瘋狂......但這個鏈接是這個網頁的URL ... – Urda 2010-03-12 15:06:13

+0

@Urda Doh!更新 :) – Gordon 2010-03-12 15:52:07

5
<?php 
class AlternativeController extends Zend_Controller_Action 
{ 
    /** 
    * Redirector - defined for code completion 
    * 
    * @var Zend_Controller_Action_Helper_Redirector 
    */ 
    protected $_redirector = null; 

    public function init() 
    { 
     $this->_redirector = $this->_helper->getHelper('Redirector'); 
    } 

    public function myAction() 
    { 
     /* Some Awesome Code */ 

     $this->redirector('targetAction', 'targetController'); 
     return; //Never reached! 
    } 
} 

你需要得到重定向幫手,那麼你可以定義targetAction和targetController與重定向。這應該做到這一點。

+0

在你的代碼中你可以使用'$ this - > _ redirect',但是如果你看在我的代碼中,你會看到我正在寫一個資產插件控制器。 – Uffo 2010-03-12 14:06:02

+0

不管怎樣,你仍然需要得到幫手。你也可以使用'$ this - > _ redirector-> gotoUrl('/ my-controller/my-action/param1/test/param2/test2');' – Urda 2010-03-12 14:53:47

+0

我不明白爲什麼必須創建一個控制器爲了使用助手,我想在我的插件中使用該助手,而不是在我的控制器中。 – Uffo 2010-03-12 15:22:50

7

給出...的代碼

if (!isset($adminLogin->loggedin)) { 
    $baseUrl = new Zend_View_Helper_BaseUrl(); 
    $this->getResponse()->setRedirect($baseUrl->baseUrl().'/something'); 
} 

休息...代碼