Skip to content

Instantly share code, notes, and snippets.

@skie
Created December 15, 2017 16:26
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 skie/e7dd53f6339819378c57a68049ca6c0b to your computer and use it in GitHub Desktop.
Save skie/e7dd53f6339819378c57a68049ca6c0b to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Controller\AppController;
use ApiExtra\Service\Invoker;
use CakeDC\Api\Exception\ValidationException;
/**
* Blogs Controller
*
* @property \App\Model\Table\BlogsTable $Blogs
*/
class BlogsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('ApiExtra.ApiPaginator');
}
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$blogs = Invoker::call('blogs', $this);
$this->ApiPaginator->paginate($blogs, ['alias' => 'Blogs']);
$blogs = $blogs->data();
$this->set(compact('blogs'));
$this->set('_serialize', ['blogs']);
}
/**
* View method
*
* @param string|null $id Blog id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$blog = Invoker::call('blogs', $this, ['suffix' => $id]);
$this->set('blog', $blog->data());
$this->set('_serialize', ['blog']);
}
/**
* Add method
*
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$blog = $this->Blogs->newEntity();
try {
if ($this->request->is('post')) {
$blog = Invoker::call('blogs', $this);
$result = $blog->data();
if ($result) {
$this->Flash->success(__('The blog has been saved.'));
if ($this->request->is('ajax')) {
return $this->redirect([
'action' => 'view',
$result->id,
'_ext' => 'json'
]);
}
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The blog could not be saved. Please, try again.'));
}
} catch (ValidationException $e) {
debug($e->getValidationErrors());
$blog->setErrors($e->getValidationErrors());
}
// $this->set($this->Blogs->buildFormParams());
$this->set(compact('blog'));
$this->set('_serialize', ['blog']);
}
/**
* Edit method
*
* @param string|null $id Blog id.
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
try {
if ($this->request->is(['patch', 'post', 'put'])) {
$blog = Invoker::call('blogs', $this, ['suffix' => $id]);
if ($blog->data()) {
$this->Flash->success(__('The blog has been saved.'));
return $this->redirect(['action' => 'index']);
}
$blog = $blog->data();
$this->Flash->error(__('The blog could not be saved. Please, try again.'));
} else {
$blog = Invoker::call('blogs', $this, ['suffix' => $id]);
$blog = $blog->data();
}
} catch (ValidationException $e) {
$blog = $this->Blogs->newEntity($this->request->data());
$blog->setErrors($e->getValidationErrors());
}
$this->set(compact('blog'));
$this->set('_serialize', ['blog']);
}
/**
* Delete method
*
* @param string|null $id Blog id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
try {
$blog = Invoker::call('blogs', $this, ['suffix' => $id, 'method' => 'DELETE']);
$result = $blog->data();
if ($result) {
$this->Flash->success(__('The blog has been deleted.'));
} else {
$this->Flash->error(__('The blog could not be deleted. Please, try again.'));
}
} catch (Exception $e) {
$this->Flash->error(__('The blog could not be deleted. Please, check if there is something associated with it.'));
}
return $this->redirect(['action' => 'index']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment