I'm trying to set up a console route that can accept multiple email addresses. Basically what I'm wanting is a route that accepts something like:
php public/index.php run-report --email=first@example.com --email=second@example.com
I've tried:
run-report [--email=]
But that will only accept a single address. Once you put in a second --email, it fails to match the route. I can hack it by passing in a comma separated string of email addresses, but I'm looking for a way that will result in an array of values, so that I don't have to parse the parameter myself.
From looking at the source code for the simple Console router (ie. Zend\Mvc\Router\Console\Simple), it doesn't appear that this is available out of the box. The console parameter matching is only written to match unique keys in the route.
However - you could try to use the 'catchall' route type instead.
So for example, use this as your console route:
'test' => array(
'type' => 'catchall',
'options' => array(
'route' => 'test', //this isn't actually necessary
'defaults' => array(
'controller' => 'Console\Controller\Index',
'action' => 'test'
)
)
)
Then you can pass as many --email values as you want, you just have to validate them in the controller.
So running this:
php index.php test --email=test@testing.com --email=test2@testing.com
Can be interpreted in the controller:
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
)
This isn't exactly ideal as you can also get the same input by executing this (ie. passing email instead of test as the route) - because it's the catchall:
php index.php email --email=test@testing.com --email=test2@testing.com
So you'll have to validate the params directly in the controller as well.