1

我已将此 zf2 路由配置添加到标准骨架应用程序中:(编辑:这里是我的完整配置:)

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'test' => array(
                'type'    => 'literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                           'controller' => 'Application\Controller\Index',
                           'action'     => 'test',
                    ),
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'query' => array(
                        'type' => 'Query',
                        'options' => array(
                            'defaults' => array(
                                'testparam' => 'bar'
                            ),
                        ),
                    ),
                ),    
        ),

        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            //'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),

    ),
),

在控制器中,我想在 testAction 中获取请求参数:(编辑:这里是我的完整控制器):

    namespace Application\Controller;

    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            var_dump(
                    $this->params()->fromQuery(),
                    $this->getRequest()->getQuery(),
                    $this->getEvent()->getRouteMatch()->getParams()
             );

            return new ViewModel();
        }

        public function testAction(){
            var_dump(
                    $this->params()->fromQuery(),
                    $this->getRequest()->getQuery(),
                    $this->getEvent()->getRouteMatch()->getParams()
             );

            return new ViewModel();
        }
    }

URL: /test?testparam=123123
结果:参数不正确!
在这里,如果我删除 may_terminate,我希望 PARAM 为“123123” ,然后我得到

PARAM = "bar" (see default value for "testparam")

array(0) { } object(Zend\Stdlib\Parameters)#195 (1) { ["storage":"ArrayObject":private]=> array(0) { } } array(3) { ["controller"]=> string(28) "Application\Controller\Index" ["action"]=> string(4) "test" ["testparam"]=> string(3) "bar" }

URL: /?testparam=123123
结果:参数正常!

array(1) { ["testparam"]=> string(6) "123123" } object(Zend\Stdlib\Parameters)#88 (1) { ["storage":"ArrayObject":private]=> array(1) { ["testparam"]=> string(6) "123123" } } array(2) { ["controller"]=> string(28) "Application\Controller\Index" ["action"]=> string(5) "index" } 

这些都不起作用我只收到带有这个 URL 的 NULL 值:/test?testparam=testvalue

如果我提出这个请求,它会起作用:/?xxx=xycvxcv

(但我需要从内部获取参数testAction()

我确实重新安装了骨架应用程序->相同的结果(仅在indexAction(“home-route”)内可以获得请求参数...

4

2 回答 2

0

如果您使用流动路由,第一种和第二种方法应该适合您:

'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),

网址: http://localhost/project/public/?ad=23

var_dump:

数组“广告”=> 字符串“23”(长度=2)

对象(Zend\Stdlib\Parameters)[106] 公共“广告”=> 字符串“23”(长度=2)

*编辑 * : 也许你应该从数组中删除 'may_terminate' 子句

于 2013-03-15T14:37:45.090 回答
-1

   Query qoute 部分 (Zend\Mvc\Router\Http\Query) 已弃用
    将您的配置更改为如下所示
     '测试' => 数组(
                '类型' => '文字',
                '选项' => 数组(
                    '路线' => '/测试',
                    '默认值' => 数组(
                           '控制器' => '应用程序\控制器\索引',
                           '行动' => '测试',
                    ),
                )
   

于 2014-12-11T05:52:18.143 回答