不同模块的不同菜单

时间:2016-08-28 13:26:54

标签: zend-framework2

每一个人。我是ZendFramework2的新手。我有一个最简单的项目,我有两个模块---用户模块和管理模块。用户模块具有登录和注册功能,管理模块在其菜单中具有配置文件和注销功能。我想为管理员和用户模块提供不同的菜单。我在其module.config.php中使用了Navigation和make菜单,它正常工作。我还在其视图中为用户和管理模块使用了不同的布局。但是当我尝试为用户模块使用导航时,它会访问管理模块的导航。 请帮助我,因为我没有老师,也没有支持者,除了你是伟大的人和观众。

我的代码如下:

用户模块的module.config.php

<?php
return array(
'controllers' => array(
    'invokables' => array( 
        'Users\Controller\Register' => 'Users\Controller\RegisterController',
        'Users\Controller\Login' => 'Users\Controller\LoginController',             
    ),
),

 'router' => array(
    'routes' => array(
        'users' => array(
            'type'    => 'Literal',
            'options' => array( 
                'route'    => '/users',                 
                'defaults' => array(
                    'controller' => 'Users\Controller\Login',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(                     
                'login' => array(
                    'type'    => 'Segment',
                    'may_terminate' => true,
                    'options' => array(
                        'route'    => '/login[/:action]',
                        'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Users\Controller\Login',
                            'action'     => 'index',
                        ),                              
                    ),
                  ),

                 'register' => array(
                    'type'    => 'Segment',
                    'may_terminate' => true,
                    'options' => array(
                        'route'    => '/register[/:action]',
                        'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Users\Controller\Register',
                            'action'     => 'index',
                        ),
                    ),
                ),
            ),
        ),
    ),
),



'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
        'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
    ),
),

'view_manager' => array(
    'template_map' => array(
        'layout/layout_users'  => __DIR__ . '/../view/layout/layout.phtml',
     ),

    'template_path_stack' => array(
        'users' => __DIR__ . '/../view',
    ),
),

'navigation' => array(
    'default' => array(
        array(
            'label' => 'Login',
            'route' => 'login',
        ),

        array(
            'label' => 'Register',
            'route' => 'register',
        ),
    ),
),

);

用户模块的layout.phtml:

<?php echo $this->doctype(); ?>
<html lang="en">
<head>
    <meta charset="utf-8">
<?php echo $this->headTitle('ZF2 '. $this->translate('Transport Information System'))->setSeparator(' - ')->setAutoEscape(false) ?>
<?php echo $this->headMeta()
        ->appendName('viewport', 'width=device-width, initial-scale=1.0')
        ->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
    ?>
<!-- Le styles -->
    <?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                    ->prependStylesheet($this->basePath('css/style.css'))
                    ->prependStylesheet($this->basePath('css/bootstrap-theme.min.css'))
                    ->prependStylesheet($this->basePath('css/bootstrap.min.css')) ?>

    <!-- Scripts -->
    <?php echo $this->headScript()
        ->prependFile($this->basePath('js/bootstrap.min.js'))
        ->prependFile($this->basePath('js/jquery.min.js'))
        ->prependFile($this->basePath('js/respond.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
        ->prependFile($this->basePath('js/html5shiv.min.js'),   'text/javascript', array('conditional' => 'lt IE 9',))
    ; ?>

</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="<?php echo $this->url('home') ?>"><img src="<?php echo $this->basePath('img/zf2-logo.png') ?>" alt="Zend Framework 2"/>&nbsp;<?php echo $this->translate('Transport Information System') ?></a>
            </div> 

            <div class="collapse navbar-collapse">
                <?php  

                echo $this->navigation('navigation')
                          ->menu()
                          ->setMinDepth(0)
                          ->setMaxDepth(0)
                          ->setUlClass('nav navbar-nav');

                ?>
            </div>


        </div>
    </nav>
    <div class="container">
        <?php echo $this->content; ?>
        <hr>
        <footer>
            <p align="center">Copyright will be updated very soon InshaAllah@<?php echo $this->translate('All rights reserved.') ?></p>
        </footer>
    </div> <!-- /container -->
    <?php echo $this->inlineScript() ?>
</body>

管理模块的

module.config.php:

<?php 

namespace Admin;

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Admin\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),



       'profile' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/profile[/:action]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Profile',
                    'action' => 'index',
                ),
            ),
        ),

       'logout' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/users/logout',
                'defaults' => array(
                    'controller' => 'Users\Controller\Login',
                    'action' => 'logout',
                ),
            ),
        ),

'admin' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '__NAMESPACE__' => 'Admin\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(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
        'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

'controllers' => array(
    'invokables' => array(
      // 'Admin\Controller\Index' => Controller\IndexController::class
        'Admin\Controller\Index' => 'Admin\Controller\IndexController',
        'Admin\Controller\Profile' => 'Admin\Controller\ProfileController',

        'Admin\Controller\Provinces' => 'Admin\Controller\ProvincesController',
        'Admin\Controller\Districts' => 'Admin\Controller\DistrictsController',
        'Admin\Controller\Cities' => 'Admin\Controller\CitiesController',
        'Admin\Controller\Stations' => 'Admin\Controller\StationsController',           
        'Admin\Controller\Services' => 'Admin\Controller\ServicesController',
        'Admin\Controller\Vehicles' => 'Admin\Controller\VehiclesController', 

    ),
),
'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/layout_admin'           => __DIR__ . '/../view/layout/layout.phtml',
        'admin/index/index' => __DIR__ . '/../view/admin/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),

'navigation' => array(
    'default' => array(
        array(
            'label' => 'Home',
            'route' => 'home',
        ),

        array(
            'label' => 'Profile',
            'route' => 'profile',
        ),

        array(
            'label' => 'Logout',
            'route' => 'logout',
        ),

    ),  
),
);
管理模块的

layout.phtml:

<?php echo $this->doctype(); ?>
<html lang="en">
<head>
    <meta charset="utf-8">
<?php echo $this->headTitle('ZF2 '. $this->translate('Transport Information System'))->setSeparator(' - ')->setAutoEscape(false) ?>

    <?php echo $this->headMeta()
        ->appendName('viewport', 'width=device-width, initial-scale=1.0')
        ->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
    ?>

    <!-- Le styles -->
    <?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                    ->prependStylesheet($this->basePath('css/style.css'))
                    ->prependStylesheet($this->basePath('css/bootstrap-theme.min.css'))
                    ->prependStylesheet($this->basePath('css/bootstrap.min.css')) ?>

    <!-- Scripts -->
    <?php echo $this->headScript()
        ->prependFile($this->basePath('js/bootstrap.min.js'))
        ->prependFile($this->basePath('js/jquery.min.js'))
        ->prependFile($this->basePath('js/respond.min.js'), 'text/javascript', array('conditional' => 'lt IE 9',))
        ->prependFile($this->basePath('js/html5shiv.min.js'),   'text/javascript', array('conditional' => 'lt IE 9',))
    ; ?>

</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="<?php echo $this->url('home') ?>"><img src="<?php echo $this->basePath('img/zf2-logo.png') ?>" alt="Zend Framework 2"/>&nbsp;<?php echo $this->translate('Transport Information System') ?></a>
            </div>


            <div class="collapse navbar-collapse">
                <?php // <-- Update this !!

                echo $this->navigation('navigation')
                          ->menu()
                          ->setMinDepth(0)
                          ->setMaxDepth(0)
                          ->setUlClass('nav navbar-nav');

                ?>
            </div>


        </div>
    </nav>
    <div class="container">
        <?php echo $this->content; ?>
        <hr>
        <footer>
            <p align="center">Copyright will be updated very soon InshaAllah.@<?php echo $this->translate('All rights reserved.') ?></p>
        </footer>
    </div> <!-- /container -->
    <?php echo $this->inlineScript() ?>
</body>

1 个答案:

答案 0 :(得分:0)

我正在查看我的代码,因为我有一个管理导航菜单。

在您的管理模块中,您需要以下文件,我已将其保存在Zend \ Navigation \ Service文件夹中。

namespace Admin\Zend\Navigation\Service;

use Zend\Navigation\Service\DefaultNavigationFactory;

class AdminNavigationFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
        return 'admin_navigation';
    }
}

在你的module.config中,你需要。

'navigation' => array(
    'admin_navigation' => array(
        array(
            'label' => 'Home',
            'route' => 'home',
        ),

        array(
            'label' => 'Profile',
            'route' => 'profile',
        ),

        array(
            'label' => 'Logout',
            'route' => 'logout',
        ),

    ),  
),

最后在你需要的管理模块layout.phtml文件中。

echo $this->navigation('admin_navigation')
                          ->menu()
                          ->setMinDepth(0)
                          ->setMaxDepth(0)
                          ->setUlClass('nav navbar-nav');

希望这会让它适合你。

您可能还想观看此Zend Framework 2 Secondary Navigation With Acl