1

I have this controller which will simply change the language in a session so I can set it in the bootstrap.

Except I want to change '$this->_redirect ( 'library/recipes/list' );' to be the URL of the page they are on. Ive tried a few functions and they dont seem to work.

Im a newbie Zend user, thanks!

class Library_LanguageswitchController extends Zend_Controller_Action {

public function init() {
  $this->_helper->layout->disableLayout ();
  $this->_helper->viewRenderer->setNoRender ();
 }

 public function switchAction() {
  $session = new Zend_Session_Namespace ( 'whatcould' );
  $session->language = $this->_getParam ( 'lang' );

  $this->_redirect ( 'library/recipes/list' );
 }

}

6 Answers 6

4

There is no built-in way to do this afaik. You want to redirect back to the referer, which may or may not be stored in $_SERVER['HTTP_REFERER'].

The best approach I can think of is writing a Zend_Controller_Action_Helper with a method signature like this

// Returns the referer from $_SERVER array or $fallback if referer is empty
public function getReferer($fallback);

// proxy to getReferer()
public function direct($fallback);

Then you could use

public function switchLanguageAction 
{
    // ... insert code to switch the language for this user ...
    $this->_redirect( $this->_helper->referer('/fallback/to/url') );
}

As an alternative, you could use a custom redirect helper that can achieve the same in one go.

Sign up to request clarification or add additional context in comments.

3 Comments

Ive just created that function in the controller itself, thanks!
@bluedaniel you're welcome. as long as you only need it in one action, it's fine inside the controller. If you need it in multiple controllers, a helper is the better way because you don't need to duplicate code.
yeah I understand that, I'll probably end up moving it a bit later but its fine for now. Being new to Zend, it seems at times Zend makes easy tasks much harder than they ought to be.
0

http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelper.redirector.basicusage mentions $this->_redirector->redirectAndExit();

Comments

0

I think that maybe you are looking at this from the wrong angle. A controller is fired after the routing has completed. From what I am seeing you really want to change the language and then route to the controller. This would be achieved via customizing the front controller and tapping in to one of the events there.

Comments

0

Another simple way to grab/set the referer is via adding a param to your frontController in your Bootstrap code:

$frontController = Zend_Controller_Front::getInstance();
$frontController->setParam('referer', $_SERVER['HTTP_REFERER']);

and then grab the referer from your controller like so:

$referer = $this->getInvokeArg('referer');

From the Zend docs for Zend_Controller_Front:

7.3.4. Front Controller Parameters

In the introduction, we indicated that the front controller also acts as a registry for the various controller components. It does so through a family of "param" methods. These methods allow you to register arbitrary data – objects and variables – with the front controller to be retrieved at any time in the dispatch chain. These values are passed on to the router, dispatcher, and action controllers. 

Comments

0

Zend not have function for this. But simple way, save url in session and use it here

2 Comments

I dont want to store the url in a session, there must be another way?
See Gordon answer, it's one of another way. But if you want redirect more than one page back, you need store url in session
0

You can use something like:

$current_page_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.