duangan6133 2015-05-17 23:39
浏览 50

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

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.

  • 写回答

1条回答 默认 最新

  • doudao1282 2015-07-15 20:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思