hide module and action name from zf2 routing

2019-06-07 06:53发布

<?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',
            ),
        ),
    ),

);
?>

i have a route like this http://www.abc.com/member-profile/756 but i want to show it like this http://www.abc.com/john. showing user first name in the url. Thanks

1条回答
2楼-- · 2019-06-07 07:04

If you want to use firstname in url by hiding module and action from url, you can do this by add routing as below:

            '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',
                    ),
                ),
            ),

You need to call this by adding this line:

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

you need to use a unique/distinct first name for every member to identify the user or provide an id in parameter to identify the member.

查看更多
登录 后发表回答