Skip to content

Instantly share code, notes, and snippets.

@cakephp-tutorial
Last active February 27, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cakephp-tutorial/aed25923217f98a426f7 to your computer and use it in GitHub Desktop.
Save cakephp-tutorial/aed25923217f98a426f7 to your computer and use it in GitHub Desktop.
<?php
#/app/controller/AuthorsController.php
class AuthorsController extends AppController {
public function index()
{
$this->Author->recursive = 1;
$authors = $this->Author->find('all');
$this->set('authors', $authors);
}
public function add()
{
if ($this->request->is('post')) {
$this->Author->create();
if ($this->Author->save($this->request->data)) {
$this->Session->setFlash(__('Inserimento avvenuto con successo'));
$this->redirect(array('controller'=>'authors','action'=>'index'));
}
$this->Session->setFlash(__('Inserimento fallito.'));
}
}
public function edit($id = null)
{
$this->Author->id = $id;
if(!$this->Author->exists()) {
$this->setFlash(__('Autore non trovato'),'flash-error');
$this->redirect(array('controller'=>'authors','action'=>'index'));
exit;
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Author->save($this->request->data)) {
$this->Session->setFlash(__('Aggiornamento avvenuto con successo'), 'flash-success');
$this->redirect(array('action' => 'index'));
exit();
}
$this->Session->setFlash(__('Problemi in fase di Aggiornamento.'), 'flash-error');
}
$this->set('author', $this->Author->findById($id));
}
public function delete($id = null)
{
$this->layoyt = false;
        $this->auotRender = false;
        if ($this->request->is('get')){
            throw new MethodNotAllowedException();
        }
if ($this->Author->delete($id)) {
$this->Session->setFlash(__('Cancellazione avvenuta con successo'), 'flash-success');
$this->redirect(array('action' => 'index'));
exit;
}
$this->Session->setFlash(__('Problemi in fase di eliminazione'), 'flash-error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment