我已将此 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”)内可以获得请求参数...