2010-03-12 124 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'); 
} 

休息...代码