简体   繁体   English

如何为 Zend 框架 2 静态路由创建可选路由参数

[英]how to create an optional route parameter for zend framework 2 restful route

I'm working with zend framework 2 and I need to create an optional parameter for a route segment that also has two required parameters.我正在使用 zend 框架 2,我需要为一个也有两个必需参数的路由段创建一个可选参数。 Below is a snippet from the module.config.php describing the route.下面是描述路由的 module.config.php 的一个片段。 My understanding is that in ZF2 an optional route parameter can be created by using the我的理解是,在 ZF2 中,可以使用

[/:param]

which you can see is what I have.你可以看到的是我所拥有的。 It works fine as long as I send the optional param, but when I leave it out the first two params "uname and appname" are appended together into the "uname" constraint.只要我发送可选参数,它就可以正常工作,但是当我将其省略时,前两个参数“uname 和 appname”将一起附加到“uname”约束中。 The route is a parent route.该路由是父路由。

'roles' => array(
            'type' => 'segment',
            'options' => array(
              'route' => '/roles/:uname/:appname[/:locnames]',
              'constraints' => array(
                'uname' => '[a-zA-Z].+',
                'appname' => '[a-zA-Z0-9_-].+',
                'locnames' => 'locnames'
             ),
             'defaults' => array(
                 'controller' => 'Roles/Controller/RolesController'
              ),
            ),
        ),

What am I missing here, I know you can have define optional parameters, but I can't figure out the correct format我在这里错过了什么,我知道您可以定义可选参数,但我无法弄清楚正确的格式

Thanks to grizzm0 on #zftalk or helping me with this one.感谢#zftalk 上的 grizzm0 或帮助我解决这个问题。 It was a simple regular expressions issue.这是一个简单的正则表达式问题。 Removing the dot(.) in the constraints correctly matched the incoming url parameters.删除约束中的点(.)与传入的 url 参数正确匹配。 So my route now looks like this:所以我的路线现在看起来像这样:

'roles' => array(
    'type' => 'segment',
    'options' => array(
      'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
      'constraints' => array(
        'uname' => '[a-zA-Z]+',
        'appname' => '[a-zA-Z0-9_-]+',
        'locnames' => 'locnames'
     ),
     'defaults' => array(
         'controller' => 'Roles/Controller/RolesController'
      ),
    ),
),
'roles' => array(
        'type' => 'segment',
        'options' => array(
          'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
          'constraints' => array(
            'uname' => '[a-zA-Z].+',
            'appname' => '[a-zA-Z0-9_-].+',
            'locnames' => 'locnames'
         ),
         'defaults' => array(
             'controller' => 'Roles/Controller/RolesController'
          ),
        ),
    ),

You can configure your route like this way.您可以像这样配置您的路线。 Here inside your roles controller you have some action live index.在您的角色控制器中,您有一些动作实时索引。 so your route will be siteurl/roles/index/uname/john/appname/stackexchange/locanames/yourlocanames所以你的路线将是 siteurl/roles/index/uname/john/appname/stackexchange/locanames/youurlocanames

here if you don't want to write appname then remove youre paramater so your route will work.在这里,如果你不想写 appname 然后删除你的参数,这样你的路线就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM