我遵循CakePHP的教程,我已經爲一個數據庫表生成控制器,視圖和模型。我也實現了在數據庫中插入新值的add方法,並且一切正常。 現在,我嘗試使用蛋糕烘烤來做同樣的事情,並且在使用基本的crud方法自動生成控制器之後,我仍然無法添加行。這是生成的代碼:CakePHP控制器方法:無法調用它
class Admin extends AppModel {
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'username';
/**
* Display field
*
* @var string
*/
public $displayField = 'username';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'insert username',
'allowEmpty' => false
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'alphaNumeric' => array(
'rule' => array('alphaNumeric'),
//'message' => 'too long',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
),
'maxLength' => array(
'rule' => array('maxLength', 50),
'message' => 'too long',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'insert password',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'alphaNumeric' => array(
'rule' => array('alphaNumeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'maxLength' => array(
'rule' => array('maxLength', 50),
'message' => 'max lenght',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
}
<?php
App::uses('AppController', 'Controller');
/**
* Admins Controller
*
* @property Admin $Admin
* @property PaginatorComponent $Paginator
* @property SessionComponent $Session
*/
class AdminController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Session');
/**
* index method
*
* @return void
*/
public function index() {
$this->autoRender = false;
$query = $this->Admin->find('all');
$result = array();
foreach ($query as $current) {
$rs = $current['Admin'];
$to_add = array();
$to_add['username'] = $rs['username'];
$to_add['password'] = $rs['password'];
array_push($result, $to_add);
}
return json_encode($result);
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->set('admin', $this->Admin->find('first', $options));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->Admin->create();
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->request->data = $this->Admin->find('first', $options);
}
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid admin'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Admin->delete()) {
$this->Session->setFlash(__('The admin has been deleted.'));
} else {
$this->Session->setFlash(__('The admin could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
$this->Admin->recursive = 0;
$this->set('admins', $this->Paginator->paginate());
}
/**
* admin_view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_view($id = null) {
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->set('admin', $this->Admin->find('first', $options));
}
/**
* admin_add method
*
* @return void
*/
public function admin_add() {
if ($this->request->is('post')) {
$this->Admin->create();
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
}
}
/**
* admin_edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_edit($id = null) {
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->request->data = $this->Admin->find('first', $options);
}
}
/**
* admin_delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function admin_delete($id = null) {
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid admin'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Admin->delete()) {
$this->Session->setFlash(__('The admin has been deleted.'));
} else {
$this->Session->setFlash(__('The admin could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
,這是簡單的添加視圖:
echo $this->Form->create(array('type' => 'post'));
echo $this->Form->input('username', array('label' => 'username', 'type' => 'text'));
echo $this->Form->input('password');
echo $this->Form->end('Save admin');
現在,當我嘗試調用
/admin/add
作秀輸入形式,我得到這個錯誤消息:
Error: AdminAddController could not be found. Error: Create the class AddController below in file: app\Controller\AddController.php
wha不對?