Zend:获取当前用于Controller的URL

时间:2010-02-24 13:51:07

标签: php zend-framework url controller

我有这个控制器只会改变会话中的语言,所以我可以在bootstrap中设置它。

除非我想更改'$ this-> _redirect('library / recipes / list');'成为他们所在页面的URL。香港专业教育学院尝试了一些功能,但它们似乎没有用。

我是新手Zend用户,谢谢!

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 个答案:

答案 0 :(得分:4)

没有内置的方法可以做到这一点。您想要重定向回引用,可能会也可能不会存储在$_SERVER['HTTP_REFERER']中。

我能想到的最好的方法是使用像这样的方法签名编写Zend_Controller_Action_Helper

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

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

然后你可以使用

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

作为替代方案,您可以使用可以一次完成相同操作的自定义重定向帮助程序。

答案 1 :(得分:0)

Zend没有这方面的功能。但简单的方法是,在会话中保存网址并在此处使用

答案 2 :(得分:0)

答案 3 :(得分:0)

我想也许你是从错误的角度看待这个。路由完成后触发控制器。从我所看到的你真的想要改变语言,然后路由到控制器。这可以通过自定义前端控制器并接入其中一个事件来实现。

答案 4 :(得分:0)

获取/设置referer的另一种简单方法是在Bootstrap代码中向frontController添加一个param:

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

然后从控制器中抓取引用程序,如下所示:

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

来自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. 

答案 5 :(得分:0)

您可以使用以下内容:

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