Khalid Mushtaq
Khalid Mushtaq

Reputation: 123

ZF2 Module based Layouts

I'm trying to learn Zend Framework 2 and currently facing an issue.

I have created a module named "Admin" and have defined layout for Admin Module. Now problem is Application module is also loading Admin Module's layout. If I browse Admin or Application module, same layout is being loaded. I have tried multiple solutions by Googling but didn't get anyone working.

I have created Admin module by copying Application module dir and renaming it to Admin and changed "Application" to "Admin" in sub directories name and code files.

Upvotes: 0

Views: 1445

Answers (2)

amarjit singh
amarjit singh

Reputation: 461

This is the visual presentation of Khalids post with one more addition in the config/application.config.php file Step 1. Step 2. Copy Application Module to and rename to Admin

//config/application.config.php
    'modules' => array(
            'Application'
            ,'Test', // add this line
        ),




<?php
//Step 3.

//your Test/config/module.config.php should look like as follows

return array(
    'router' => array(
        'routes' => array(
            /* 'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                       // 'controller' => 'StickyNotes\Controller\Album', 
                       //'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ), */
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Test\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Test\Controller\Index' => 'Test\Controller\IndexController',

        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/test'           => __DIR__ . '/../view/layout/test.phtml',
            'application/index/index' => __DIR__ . '/../view/test/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

Step 4. and finally your Test/Module file should look as follows:->

namespace Test;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $e->getApplication()->getServiceManager()->get('translator');
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

and you will be ready to roll.

Upvotes: 1

Khalid Mushtaq
Khalid Mushtaq

Reputation: 123

Well, at last I have found a solution.

Suppose you want to create a module named "Admin", here are the steps:

1- Copy Application Module Dir and rename it to "Admin". It is the name of your module.

2- Update all references in Admin Module that are initially pointing towards Application module. ( change "Application" to "Application" and "application" to "admin" )

3- In Admin/config/module.config.php remove "home" route.

4- Update your layout and views.

5- Test in your browser by using http://example.com/admin

That's it.

You don't need any external layout lib like "EdpModuleLayouts"

Cheers :)

Upvotes: 0

Related Questions