ZF2如何将URL段作为Action params传递

时间:2013-07-25 19:57:45

标签: routing zend-framework2

我的网址如下:

  

http://www.example.com/r/4442323222344/HLKKLKLKLKLKLKH

我想将网址的两个网段(4442323222344和HLKKLKLKLKLKLKH)映射为 redirectAction 功能的参数,因此 $ messageId 为“ 4442323222344 “和 $ hash 是” HLKKLKLKLKLKLKH

这是路线:

    'router' => array(
        'routes' => array(
             ....
            'redirect' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/r',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'redirect',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/:messageId/:hash',
                            'constraints' => array(
                                'messageId' => '[a-zA-Z0-9]+',
                                'hash'     => '[a-zA-Z0-9]+',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
            ...
        ),
   ),

这就是行动:

class IndexController extends AbstractActionController implements ConfigAwareInterface
{
    ...
    public function redirectAction($messageId,$hash)
    {
      ...
      myFunc($messageId,$hash);
      ...
    }
    ...
}

我知道我可以使用这些片段:

$_messageId = $this->params()->fromRoute('messageId');
$_hash = $this->params()->fromRoute('hash');

但我发现通过函数参数传递变量更加“干净”。

1 个答案:

答案 0 :(得分:1)

目前无法像这样做。这是一个改进ZF3的想法(不会很快发布,所以不用担心),但在ZF2中你不会看到这个功能发布。

您需要立即使用params() - 插件

相关问题