Php Zend:如何向控制器操作添加/获取URL参数

Php Zend:如何向控制器操作添加/获取URL参数,php,zend-framework2,Php,Zend Framework2,我有一个文件控制器,它有两个动作:上传和下载 它的定义如下: 'files' => array( 'type' => 'Segment', 'options' => array( 'route' => '/files[/:action]', 'defaults' => array( 'controller' => 'Application\Controller\Files',

我有一个文件控制器,它有两个动作:上传和下载

它的定义如下:

'files' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/files[/:action]',
        'defaults' => array(
            'controller' => 'Application\Controller\Files',
            'action' => 'index',
        ),
    ),
),
我希望下载操作可以像/files/download/1?authString=asdf一样访问。在本例中,1是一个文件ID

我知道我可以将路由更改为
/files[/action[/:fileId]]
来设置路由,如果我错了,请纠正我,但是如何访问downloadAction中的fileId?还有什么我需要改变的路线定义,使其工作

我可以将路由更改为
/files[/action[/:fileId]]
要设置路由,请更正我的错误

你没有错,那是一条有效的路线

路线定义还有什么需要更改的吗

如果将
fileId
添加为可选路由参数,则需要在
downloadAction()
中进行一些手动检查,以确保已设置该参数

另一个解决方案是将路由分为子路由,这样可以确保除非每个路由上有正确的参数,否则不会匹配

'files' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/files',
        'defaults' => array(
            'controller' => 'Application\Controller\Files',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(

        'download' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/download/:fileId',
                'defaults' => array(
                    'action' => 'download',
                ),
                'constraints' => array(
                    'fileId' => '[a-zA-Z0-9]+',
                ),
            ),
        ),

        'upload' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/upload',
                'defaults' => array(
                    'action' => 'upload',
                ),
            ),
        ),

    ),
),
如何访问
下载操作中的
文件ID

最简单的方法是从路由中获取参数)

或者特别是从路线上

// FilesController::downloadAction()
$fileId = $this->params()->fromRoute('fileId');
// FilesController::downloadAction()
$fileId = $this->params()->fromRoute('fileId');