有没有办法用重复参数创建ZF2控制台路由

时间:2015-05-17 23:39:39

标签: php zend-framework2 console-application

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

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

我试过了:

run-report [--email=]

但那只会接受一个地址。一旦你输入第二个 - 电子邮件,它就无法匹配路线。我可以通过传入逗号分隔的电子邮件地址来破解它,但我正在寻找一种方法来产生一系列值,这样我就不必自己解析参数了。 / p>

1 个答案:

答案 0 :(得分:0)

通过查看简单的Console路由器的源代码(即Zend \ Mvc \ Router \ Console \ Simple),它看起来并不是开箱即用的。控制台参数匹配仅用于匹配路径中的唯一键。

然而 - 您可以尝试使用'catchall'路线类型。

例如,使用它作为控制台路径:

'test'  => array(
    'type'    => 'catchall',
    'options' => array(
        'route'    => 'test',  //this isn't actually necessary
        'defaults' => array(
            'controller' => 'Console\Controller\Index',
            '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] => Console\Controller\Index
    [action] => test
)

这并不完全理想,因为您也可以通过执行此操作获得相同的输入(即通过电子邮件而不是测试作为路线) - 因为它是捕获的:

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

因此,您还必须直接在控制器中验证参数。