从zf2路由隐藏模块和操作名称

时间:2013-10-22 06:14:49

标签: zend-framework2

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Member\Controller\Member' => 'Member\Controller\MemberController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'member' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Member\Controller\Member',
                        'action' => 'index',
                    ),
                ),
            ),
            'member' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '[/:action][/:pkMemberId][/:status]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'pkMemberId' => '[0-9]+',
                        'status' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Member\Controller\Member',
                        'action' => 'index',
                    ),
                ),
            ),
            'changeMemberPassword' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/changeMemberPassword',
                    'defaults' => array(
                        'controller'    => 'Member\Controller\Member',
                        'action'        => 'changePassword',
                    ),
                ),
            ),  
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type' => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern' => '%s.mo',
            ),
        ),
    ),

);
?>

我有这样的路线http://www.abc.com/member-profile/756,但我希望像http://www.abc.com/john一样展示它。在网址中显示用户名。感谢

1 个答案:

答案 0 :(得分:1)

如果您想通过隐藏url中的模块和操作来在url中使用firstname,可以通过添加路由来执行此操作:

            'member' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '[/:firstname][/:pkMemberId][/:status]',
                    'constraints' => array(
                        'firstname' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'pkMemberId' => '[0-9]+',
                        'status' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Member\Controller\Member',
                        'action' => 'view',
                    ),
                ),
            ),

你需要通过添加以下行来调用它:

<a href="<?php echo $this->url('member', array('firstname' => 'harish'), array('force_canonical' => true)) ?>">harish</a>

您需要为每个成员使用唯一/不同的名字来标识用户或在参数中提供ID以标识该成员。