如何为zend framework 2 restful route创建一个可选的route参数

时间:2021-12-31 07:42:09

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. Below is a snippet from the module.config.php describing the route. My understanding is that in ZF2 an optional route parameter can be created by using the

我正在使用zend框架2,我需要为路径段创建一个可选参数,该路径段也有两个必需参数。下面是module.config.php中描述路由的片段。我的理解是,在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. The route is a parent route.

你能看到的就是我所拥有的。只要我发送可选参数,它就可以正常工作,但是当我把它放在前两个参数时,“uname和appname”会被附加到“uname”约束中。该路线是父路线。

'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

我在这里缺少什么,我知道你可以定义可选参数,但我无法弄清楚正确的格式

2 个解决方案

#1


1  

Thanks to grizzm0 on #zftalk or helping me with this one. It was a simple regular expressions issue. Removing the dot(.) in the constraints correctly matched the incoming url parameters. So my route now looks like this:

感谢#zftalk上的grizzm0或帮我这个。这是一个简单的正则表达式问题。删除约束中的点(。)正确匹配传入的url参数。所以我的路线现在看起来像这样:

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

#2


0  

'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 / yourlocanames

here if you don't want to write appname then remove youre paramater so your route will work.

在这里如果你不想写appname然后删除你的参数,那么你的路线将起作用。

#1


1  

Thanks to grizzm0 on #zftalk or helping me with this one. It was a simple regular expressions issue. Removing the dot(.) in the constraints correctly matched the incoming url parameters. So my route now looks like this:

感谢#zftalk上的grizzm0或帮我这个。这是一个简单的正则表达式问题。删除约束中的点(。)正确匹配传入的url参数。所以我的路线现在看起来像这样:

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

#2


0  

'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 / yourlocanames

here if you don't want to write appname then remove youre paramater so your route will work.

在这里如果你不想写appname然后删除你的参数,那么你的路线将起作用。