我是 Zend 框架的初学者。我想我的问题很基本......但我自己无法解决。
在indexAction中,$request->isPost()总是假的。怎么了?
EntryController::indexAction
public function indexAction() {
$form = new AgreementForm();
$form->get('submit')->setValue('Go Entry Form');
$request = $this->getRequest();
if ($request->isPost()) {
var_dump('//// $request->isPost() is true //////');
if ($form->get('agreementCheck')) {
// Redirect to list of entries
return $this->redirect()->toRoute('entry');
} else {
return array('form' => $form);
}
} else {
var_dump('//// $request->isPost() is false //////');
return array('form' => $form);
}
}
index.phtml 中的表格
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCheckbox($form->get('agreementCheck'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
AgreementForm是使用代码生成器生成的。 http://zend-form-generator.123easywebsites.com/formgen/create 如下。
class AgreementForm extends Form {
public function __construct($name = null) {
parent::__construct('');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'agreementCheck',
'type' => 'Zend\Form\Element\MultiCheckbox',
'attributes' => array(
'required' => 'required',
'value' => '0',
),
'options' => array(
'label' => 'Checkboxes Label',
'value_options' => array(
'0' => 'Checkbox',
),
),
));
$this->add(array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
请告诉我一些提示。
更新: 开发者工具分析结果,POST和GET同时工作。
更新: 路由器定义@module.config.php 就是这个。
'router' => array(
'routes' => array(
'entry' => array(
'type' => 'segment',
'options' => array(
'route' => '/entry[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
),
),