Reputation: 10068
I've follow tutorial for CakePHP and i've generate a controller, a view, and a model for one database table. I've also implemented the add method for insert new values inside database, and everything works. Now, i've try to use cake bake to do the same thing, and after auto genereate controller with basic crud methods, i'm still not able to do add rows. This is the generated code:
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'));
}
}
and this is simple add 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');
now, when i try to invoke
/admin/add
for show input form, i obtain this error message:
Error: AdminAddController could not be found. Error: Create the class AddController below in file: app\Controller\AddController.php
what's wrong?
Upvotes: 0
Views: 185
Reputation: 2470
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
Upvotes: 1
Reputation: 11693
You should Always make controller name plural like :
AdminsController
And call like /admins/add
. This is basic cakePHP steps. Read cakePHP cook book.
Upvotes: 1