Tag Archives: redirect

Redirect with Zend Framework

Once I wrote about redirecting with Zend Framework, but what I missed back than was a common mistake. Although the code from my post is working, to be completely correct after redirecting I’d to add an exit() statement.

This is important because the header is changed and if something goes after the redirect there will be some problems. Finally the correct snippet is:

public function() {
	$this->_redirect('some_url');
	exit();	
}

Redirecting with Zend Framework – part 2

Recently I posted about the redirecting mechanism of Zend Framework. It’s a working chunk of code but the problem is that it seems quite difficult in according with other ZF sample codes.

Yes the same goal can be achieved with less effort. Like the _forward function which just executes other action/controller, you can call the _redirect method to rewrite the URI.

Just use that in any action of any controller:

$this->_redirect('http://www.example.com');

That executes exactly the same as calling the header PHP method or echoing the <meta> refresher!

Redirecting with Zend Framework

Many of the web applications need to be redirected to some given URI sooner or later. In that scenario like redirecting to the profile page after login, which is very common to almost every app, you need to change the URI of the browser after the login form is validated. You can make this by simply using the built in header function in PHP. In other terms the most popular method to redirect in Zend Framework, don’t know why, but this is the _forward method of the controller actions:

$this->_forward('action-name');

The problem here is that thus you get to the given action name, or controller/action but the URI of the browser stays as it is from the caller action.

If you’d like to redirect, like the header function will do it you should use the following construction:

$this->_helper->redirector->gotoRoute(array(
   'controller'=> 'contrlname',
   'action' =>'actionname'));