Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 如何在多个视图中使用同一表单_Cakephp_Dry - Fatal编程技术网

Cakephp 如何在多个视图中使用同一表单

Cakephp 如何在多个视图中使用同一表单,cakephp,dry,Cakephp,Dry,我使用的是CakePHP2.4。我有一个博客,我可以添加和编辑文章。当我实现我的edit.ctp时,我意识到我在视图add.ctp中有相同的代码: <?php echo $this->Form->create(); echo $this->Form->input('headline'); echo $this->Form->input('text', array('type' => 'textarea'); echo

我使用的是CakePHP2.4。我有一个博客,我可以添加和编辑文章。当我实现我的
edit.ctp
时,我意识到我在视图
add.ctp
中有相同的代码:

<?php
    echo $this->Form->create();
    echo $this->Form->input('headline');
    echo $this->Form->input('text', array('type' => 'textarea');
    echo $this->Form->end('Save');
?>

使用表单代码在文件夹元素中创建视图

// app/View/Elements/postForm.ctp

<?php
    echo $this->Form->create();
    echo $this->Form->input('headline');
    echo $this->Form->input('text', array('type' => 'textarea');
    echo $this->Form->end('Save'); 
?>

使用表单代码在文件夹元素中创建视图

// app/View/Elements/postForm.ctp

<?php
    echo $this->Form->create();
    echo $this->Form->input('headline');
    echo $this->Form->input('text', array('type' => 'textarea');
    echo $this->Form->end('Save'); 
?>

?您还可以使用$this->render('edit')渲染视图;在add中,如果您有edit.ctp,它的代码与您希望在add.ctp中使用的代码相同,您还可以使用$this->render('edit')渲染视图;如果您有edit.ctp,它的代码与您希望在CakePHP中的add.ctpWorks中使用的代码相同,则可以在add中使用。谢谢。如果“添加”和“编辑”完全相同,只需在控制器中的“添加/编辑”操作中覆盖要呈现的视图:
$this->render('postForm')如果您创建100%相同的表单作为元素,您将得到添加和编辑视图,这些视图只有一行
echo$this->element('postForm'),这有点浪费。@Kai:谢谢你的评论。我在视图编辑和添加中使用100%相同的表单。但这两种观点并非100%相同。除了形式不同之外,它们在一些html-elements中是不同的。谢谢。如果“添加”和“编辑”完全相同,只需在控制器中的“添加/编辑”操作中覆盖要呈现的视图:
$this->render('postForm')如果您创建100%相同的表单作为元素,您将得到添加和编辑视图,这些视图只有一行
echo$this->element('postForm'),这有点浪费。@Kai:谢谢你的评论。我在视图编辑和添加中使用100%相同的表单。但这两种观点并非100%相同。除了表单之外,它们在一些html元素中有所不同。