Search code examples
phpzend-frameworkzend-framework3zf3

ZF3 redirect to another controller acton in another module


I´m using ZF3 with the Applcation and User modules. The Application module is the default loaded module that contains my index page as a welcome page with no login information. The User module has the login page and all the user authentication stuff.

I need to put a button in my application modula index action page to redirect to the login page at user module index page.

I´m trying to use the answer from this post but it does not seen to work.

<p>
<a class="btn btn-default" href="
    <?= $this->getHelper(redirector)->gotoSimple('index', 'index', 'user'); ?>">
    Other module index action
</a>
</p>

This is leading to the following error:

Zend\ServiceManager\Exception\ServiceNotFoundException

File:

/path/to/project/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php 133

Message:

A plugin by the name "getHelper" was not found in the plugin manager Zend\View\HelperPluginManager

How should I redirect to an action of another module using ZF3 ?


Solution

  • The post you linked is zendframework 1. There have been a lot of changes made to ZF3 since then.

    Also a redirect is something you do inside the controller. e.g. if something is saved successfully to the database you can redirect, within the same request, to another page with a message or view the saved changes.

    What you are looking for is the url helper. You use it inside a zend-view template to generate an url.

    <a href="<?= $this->url('news', ['action' => 'details', 'id' =>42]); ?>">
        Details of News #42
    </a>