【问题标题】:CakePHP controller method: unable to invoke itCakePHP 控制器方法:无法调用它
【发布时间】:2015-02-24 10:15:37
【问题描述】:

我遵循 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'));
}

}

这是简单的添加视图:

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

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

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

怎么了?

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    在 cakephp 中,控制器名称总是复数,模型名称是单数,数据库表名称是复数,例如您的表是 admins,模型名称必须是 Admin。控制器名称 AdminsController 带有驼峰帽,在 Admins 末尾,必须添加 Controller,因为 AdminsController 继承了 AppController 类的属性 你应该把它写在你的控制器中

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

    还有你的地址栏

    /Admins/add
    

    【讨论】:

      【解决方案2】:

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

      AdminsController
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 2018-12-18
        • 1970-01-01
        相关资源
        最近更新 更多