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

时间:2015-02-24 10:15:38

标签: php cakephp

我已经关注了CakePHP的教程,并为一个数据库表生成了一个控制器,一个视图和一个模型。我还实现了在数据库中插入新值的add方法,一切正常。 现在,我尝试使用蛋糕烘焙来做同样的事情,并且在使用基本的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'));
}

}

这很简单,添加View:

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

对于show输入表单,我收到此错误消息:

  

错误:找不到AdminAddController。错误:创建类   下面的AddController在文件中:app \ Controller \ AddController.php

出了什么问题?

2 个答案:

答案 0 :(得分:1)

您应始终将控制器名称复数形式为:

AdminsController

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

答案 1 :(得分:1)

  

在cakephp中,控制器名称总是复数,模型名称是单数,数据库表名称是复数,例如您的表格是admins&amp;型号名称必须为Admin。控制器名称AdminsController与camel cap和管理员端,必须添加Controller,因为AdminsController继承了AppController类的属性   你应该在你的控制器中写这个

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

和您的地址栏

/Admins/add