CakePHP 控制器方法:无法调用它

CakePHP controller method: unable to invoke it

我已经按照 CakePHP 教程进行操作,并且为一个数据库生成了一个控制器、一个视图和一个模型 table。我还实现了在数据库中插入新值的添加方法,一切正常。 现在,我尝试使用蛋糕烘焙来做同样的事情,并且在使用基本的 crud 方法自动生成控制器之后,我仍然无法添加行。这是生成的代码:

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

怎么了?

您应该始终将控制器名称设为复数形式,例如:

AdminsController

并像 /admins/add 一样打电话。这是基本的 cakePHP 步骤。阅读 cakePHP 烹饪书。

In cakephp always Controller name are plural , Model name is singular and database table name are plural for example your table is admins & the Model name must be Admin. controller name AdminsController with camel caps and at the End of Admins, Controller must be added because AdminsController are inherit the properties of AppController class you should write this in your controller

class AdminsController extends AppController {
/* your action */
}

and your address bar

/Admins/add