从 zf2 路由隐藏模块和操作名称 [英] hide module and action name from zf2 routing

查看:28
本文介绍了从 zf2 路由隐藏模块和操作名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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.在 url 中显示用户名.谢谢

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

推荐答案

如果你想通过在 url 中隐藏模块和动作来使用 url 中的名字,你可以通过添加如下路由来实现:

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>

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

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.

这篇关于从 zf2 路由隐藏模块和操作名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆