Domanda

This is my Zend Framework directory structure (only included what you'll need):

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

In the IndexController I want to automatically redirect them to the home view.

How do I achieve this?

This is what I have tried so far:

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

I want to redirect to a different view and controller altogether. Not a different action within the same controller.

È stato utile?

Soluzione

you have some options:

use the _redirect action utility method (very common)

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

or use the _forward action utility method to just execute the requested action without actually redirecting

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

you could use the action helper redirector, similar to _redirect but has many more options.

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

This about covers the basics of redirecting inside of a controller, if you want to actually render home/index.phtml from /idex/index you could use the viewrenderer action helper.

I'll let you unlock those secrets.

Altri suggerimenti

If you want to render another action inside current action it is possible within your action the following way:

$this->render('actionName');

or, explicitly set the template to render

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

UPDATE: Reading again your question I suppose you want to use

_forward($action, $controller = null, $module = null, array $params = null): perform another action. If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed.

or

_redirect($url, array $options = array()): redirect to another location. This method takes a URL and an optional set of options. By default, it performs an HTTP 302 redirect.

see documentation on this question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top