Tejaswee
Tejaswee

Reputation: 31

CANNOT Map different action and controller in Zend Framework 2

I have created a simple User Module with 3 views (index, login, register). I am trying to map them to the IndexController in User module which has following code. The code is running without any error, BUT IS NOT DISPLAYING THE TEMPLATES

Controller code in : User\src\users\Controllers\IndexController

namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Helper\ViewModel;

class IndexController extends AbstractActionController
{
    public  function indexAction()
    {
        $view = new ViewModel();
        return  $view;
    }

    public function registerAction()
    {
        $view = new ViewModel();
        $view->setTemplate('users/index/new-user.phtml');
        return $view;
    }

    public function loginAction()
    {
        $view = new ViewModel();
        $view->setTemplate('users/index/login');
        return $view;
    }
}

And the templates in View are

User\view\index\index.phtml

    <h1>Welcome to Users Module</h1>
    <a href="/users/index/login">Login</a>
    <br> <br>
    <a href="/users/index/register">New User Registration</a>

User\view\index\login.phtml

    <h2> Login </h2>
    <p> This page will hold the content for the login form </p>
    <a href="/users"><< Back to Home</a>

User\view\index\new-user.phtml

    <h2> New User Registration </h2>
    <p> This page will hold the content for the registration form </p>
    <a href="/users"><< Back to Home</a>

Upvotes: 1

Views: 132

Answers (1)

Stanimir Dimitrov
Stanimir Dimitrov

Reputation: 1890

You are missing a template map such as this.

// Generated by ZF2's ./bin/templatemap_generator.php
return [
    'application/contact/index'       => __DIR__ . '/view/application/contact/index.phtml',
    'application/index/index'         => __DIR__ . '/view/application/index/index.phtml',
    'application/login/index'         => __DIR__ . '/view/application/login/index.phtml',
    'application/login/newpassword'   => __DIR__ . '/view/application/login/newpassword.phtml',
    'application/login/resetpassword' => __DIR__ . '/view/application/login/resetpassword.phtml',
    'application/menu/post'           => __DIR__ . '/view/application/menu/post.phtml',
    'application/news/index'          => __DIR__ . '/view/application/news/index.phtml',
    'application/news/post'           => __DIR__ . '/view/application/news/post.phtml',
    'application/pagination'          => __DIR__ . '/view/application/pagination.phtml',
    'application/registration/index'  => __DIR__ . '/view/application/registration/index.phtml',
    'error/index'                     => __DIR__ . '/view/error/index.phtml',
    'layout/layout'                   => __DIR__ . '/view/layout/layout.phtml',
];

You can generate one by running this command php ../../vendor/bin/templatemap_generator.php from your module/Users folder. After that in your module.config.php file inside view_manager array put this 'template_map' => include __DIR__ . '/../template_map.php',

Upvotes: 1

Related Questions