这里我完成了联系我们页面的代码,其中包含两个文件,例如电子邮件,正文,它将值存储在db中 但它不起作用我在这里发布代码
模型
//model/pages.php
<?php
class pages extends AppModel{
var $useTable = 'contact';
}
?>
控制器
class PagesController extends AppController {
public $uses = array();
public function display() {
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
public function index(){
$this->set('post', $this->contact->find('all'));
}
public function create() {
if ($this->request->is('post'))
//this function for methods like get, post, set,delelte
{
// print_r('post');
$this->contact->create();
if ($this->contact->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
查看 这里显示代码
view/contact.ctp
<?php
echo 'welcome to Contact us';
echo $this->Form->create('pages');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save pages');
?>
在db表和值中添加联系
答案 0 :(得分:0)
您是否曾在 CakePHP (例如Getting Started或CakePHP Conventions)中阅读了一些内容?
阅读一些文档,您会发现您的表格应以复数形式命名,模型应以单数形式命名。您在app/Model/Contact.php
中的模型如下所示:
class Contact extends AppModel {
public $useTable = 'contacts'; // It is not necessary, because CakePHP implicitly understands that your table will be in the plural
}
现在,您的视图位于app/View/Contacts/index.ctp
:
echo $this->Form->create('Contact');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save');
您的控制器位于app/Controller/ContactsController.php
:
class ContactsController extends AppController {
public function index() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash(__('Your contact has been saved.'));
} else {
$this->Session->setFlash(__('Unable to add your contact.'));
}
}
}
}
这是一个基本用法。我建议您阅读文档以开始使用CakePHP,因为它有许多您可能需要的有趣内容。