0

I made a file in library/My/Utils/Utils.php. The content of the file is :

class My_Utils_Utils{
    public function test(){
        $this->_redirect('login');   
    }
}

This class is called from a layout; the problem is with the _redirect(); I get this error : The page isn't redirecting properly. My question is how call the _redirect() function from a class made by you in ZEND framework 1 . Thanks in advance.

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
Naghi Attila
  • 117
  • 1
  • 1
  • 11
  • A very simlar question here, maybe it's usefult for your need http://stackoverflow.com/questions/1913509/zend-redirect-to-action-with-parameters – Daniele Vrut Nov 05 '13 at 09:49

3 Answers3

0

Use redirect() instead of _redirect(). Usage is:

$this->redirect(<action>, <controller>, <module>, <param>);

In your case $this->redirect('login'); should do the trick.

simplyray
  • 1,200
  • 1
  • 16
  • 25
  • it doesn't :( . Actually the redirect('') function it doesnt exits in zend, maybe at codeigniter. In zend is _rediect('') with underscore. – Naghi Attila Nov 05 '13 at 12:33
  • @Naghi _redirect() method is deprecated as of Zend Framework 1.7. Use redirect() instead. – bmerigan Apr 17 '18 at 00:23
0

You can use the redirector action-helper, which you can get statically from the HelperBroker using:

// get the helper
$redirectHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

// call methods on the helper
$redirect->gotoUrl('/some/url');

It should be noted, however, that the layout is considered part of the view layer. Typically, any checks that result in a redirect should probably take place earlier in the request dispatch-cycle, typically in a controller or in a front-controller plugin.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • believe I already tried it. The fact is if I call the class and the function from a controller , it works, but if i call it from a layout it doesnt. any ideas why ? – Naghi Attila Nov 05 '13 at 12:32
  • please dont send me the documentation from zend. Im a newbie I want proper examples, not teory. thx for understanding me – Naghi Attila Nov 05 '13 at 12:37
  • Aah, probably need to use `gotoUrlAndExit()`, instead. That will actually trigger the redirect, not just set the headers in the response object. – David Weinraub Nov 06 '13 at 04:09
0

The _redirect function is provided by the class Zend_Controller_Action. You can fix this in two ways :

  1. Extend Zend_Controller_Action and use _redirect

    class My_Utils_Utils extends Zend_Controller_Action {
       public function test(){
        $this->_redirect('login');   
       }
    

    }

in layout:

     $request = Zend_Controller_Front::getInstance()->getRequest();
     $response = Zend_Controller_Front::getInstance()->getResponse()
     $util = new My_Utils_Utils($request, $response); // The constructor for Zend_Controller_Action required request and response params.
    $util->test();
  1. Use gotoUrl() function Zend_Controller_Action_Helper_Redirector::gotoUrl()

     $redirector = new Zend_Controller_Action_Helper_Redirector();
     $redirector->gotoUrl('login');
    
     //in layout : 
     $util = new My_Utils_Utils();
     $util->test();
    
janenz00
  • 3,315
  • 5
  • 28
  • 37
  • believe I already tried it. The fact is if I call the class and the function from a controller , it works, but if i call it from a layout it doesnt. – Naghi Attila Nov 05 '13 at 12:31