重定向到控制器中的其他视图


Redirect to a different view in the controller

这是我的Zend Framework目录结构(只包含你需要的东西):

-Controllers
    -HomeController.php
    -IndexController.php
-Views
    -scripts
        -home
            -index.phtml
        -index
            -index.phtml

IndexController中,我想自动将它们重定向到主视图。

我如何实现这一点?

这是我到目前为止尝试过的:

header('/application/views/home');

我想完全重定向到不同的视图和控制器。同一控制器中的操作不同。

您有一些选择:

使用_redirect操作实用程序方法(非常常见)

public function indexAction() {
    $this->_redirect('/application/views/home');
}

或者使用 _forward 操作实用工具方法仅执行请求的操作,而无需实际重定向

public function indexAction() {
        $this->_forward('index', 'home');
    }

可以使用操作帮助程序重定向器,类似于_redirect,但有更多选项。

public function indexAction() {
            $this->getHelper('Redirector')->gotoSimple('index','home');
        }

这大约涵盖了在控制器内部重定向的基础知识,如果您想从/idex/index实际渲染home/index.phtml,您可以使用视图渲染器操作助手。

我会让你解开这些秘密。

如果要

在当前动作中渲染另一个动作,可以通过以下方式在动作中呈现:

$this->render('actionName');

或者,显式设置要呈现的模板

$this->renderScript('path/to/viewscript.phtml');

更新:再次阅读您的问题,我想您想使用

_forward($action, $controller = null, $module = null, array $params = null):执行另一个操作。如果在 preDispatch() 中调用,当前请求的操作将被跳过,转而使用新操作。否则,在处理当前操作后,将执行 _forward() 中请求的操作。

_redirect($url, array $options = array()):重定向到另一个位置。此方法采用 URL 和一组可选选项。默认情况下,它执行 HTTP 302 重定向。

请参阅有关此问题的文档。