我已将此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();
}
}
网址 /test?testparam=123123
结果: PARAM不行!在这里,我希望PARAM为“123123”
如果我删除may_terminate然后我得到
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" }
网址 /?testparam=123123
结果: PARAM没问题!
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
(“主路线”)内可以获得请求参数......
答案 0 :(得分: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:
array'ad'=>字符串'23'(长度= 2)
对象(Zend \ Stdlib \ Parameters)[106] public'ad'=>字符串'23' (长度= 2)
* 编辑* :也许你应该从数组中删除'may_terminate'子句
答案 1 :(得分:-1)
The Query qoute part (Zend\Mvc\Router\Http\Query) is deprectaed change your config to look like this 'test' => array( 'type' => 'literal', 'options' => array( 'route' => '/test', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'test', ), )