我关注https://zf2-docs.readthedocs.org/en/latest/in-depth-guide/data-binding.html。
我的代码如下:
edit.phtml:
<h2>EDIT FORM</h2>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('post/edit', array('id' => $this->id), true));
$form->prepare();
$form->get('submit')->setValue('Update Post');
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
WriteController.php:(行动部分)
public function editAction()
{
$request = $this->getRequest();
$post = $this->postService->findPost($this->params('id'));
$this->postForm->bind($post);
if ($request->isPost()) {
$this->postForm->setData($request->getPost());
if ($this->postForm->isValid()) {
try {
$this->postService->savePost($post);
return $this->redirect()->toRoute('blog');
} catch (\Exception $e) {
die($e->getMessage());
// Some DB Error happened, log it and let the user know
}
}
}
return new ViewModel(array(
'form' => $this->postForm,
'id'=>$this->params('id')
));
}
module.config.php:(路线部分)
'router'=>array(
'routes'=>array(
'post'=>array(
'type'=>'literal',
'options'=>array(
'route'=>'/blog',
'defaults'=>array(
'controller'=>'Blog\Controller\List',
'action'=>'index'
)
),
'may_terminate'=>true,
'child_routes'=>array(
'detail'=>array(
'type'=>'segment',
'options'=>array(
'route'=>'/:id',
'defaults'=>array(
'action'=>'detail'
),
'constraints'=>array(
'id'=>'\d+'
)
)
),
'add'=>array(
'type'=>'literal',
'options'=>array(
'route'=>'/add',
'defaults'=>array(
'controller'=>'Blog\Controller\Write',
'action'=>'add'
)
)
),
'edit' => array(
'may_terminate'=>true,
'type' => 'segment',
'options' => array(
'route' => '/edit/:id',
'defaults' => array(
'controller' => 'Blog\Controller\Write',
'action' => 'edit'
),
'constraints' => array(
'id' => '\d+'
)
)
),
)
),
)
),
问题:
当我尝试像http://www.myzend.com/blog/edit/2
那样获取可编辑的表单时,我收到以下错误。
路线名称&#34;博客&#34;找不到
注意:当我只在edit.phtml
中放入静态html时,正确显示静态内容的页面。
我正在使用Zend Framework 2.3.3
如果您需要更多详细信息,请与我们联系。