ZF2如何将URL段作为Action参数传递



我得到了URLS,比如:

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

我想将URL的两个段(4442323222344和HLKKLKLKLKLKH)映射为我的redirectAction函数的参数,因此$messageId为"444232322234",$hash是"HLKKLKlkLKH"

这是路线:

    'router' => array(
        'routes' => array(
             ....
            'redirect' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/r',
                    'defaults' => array(
                        '__NAMESPACE__' => 'ApplicationController',
                        'controller' => 'ApplicationControllerIndex',
                        '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');

但我发现通过函数params传递变量更"干净"。

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

您现在需要使用params()插件

最新更新