是否有方法创建具有重复参数的ZF2控制台路由?



我正在尝试设置一个可以接受多个电子邮件地址的控制台路由。基本上,我想要的是一个接受如下内容的路由:

php public/index.php run-report --email=first@example.com --email=second@example.com

我试过:

run-report [--email=]

但是它只接受一个地址。一旦你放入第二封电子邮件,它就无法匹配路由。我可以通过传入一个逗号分隔的电子邮件地址字符串来破解它,但我正在寻找一种方法,可以产生一个值数组,这样我就不必自己解析参数了。

查看简单控制台路由器的源代码(即。ZendMvcRouterConsoleSimple),这似乎不是开箱即用的。console参数匹配只写入匹配路由中的唯一键。

但是,你可以尝试使用'catchall'路由类型。

例如,使用这个作为你的控制台路由:

'test'  => array(
    'type'    => 'catchall',
    'options' => array(
        'route'    => 'test',  //this isn't actually necessary
        'defaults' => array(
            'controller' => 'ConsoleControllerIndex',
            'action'     => 'test'
        )
    )
)

然后你可以传递任意多的——email值,你只需要在控制器中验证它们。

所以运行这个:

php index.php test --email=test@testing.com --email=test2@testing.com

可以在控制器中解释:

print_r( $this->getRequest()->getParams()->toArray() );
Array
(
    [0] => test
    [1] => --email=test@testing.com
    [2] => --email=test2@testing.com
    [controller] => ConsoleControllerIndex
    [action] => test
)

这不是完全理想的,因为你也可以通过执行这个(即。传递电子邮件而不是测试作为路由)——因为它是包罗万象的:

php index.php email --email=test@testing.com --email=test2@testing.com

所以你必须直接在控制器中验证参数

最新更新