Skip to content

Instantly share code, notes, and snippets.

@alexi190
Last active August 29, 2015 14:26
Show Gist options
  • Save alexi190/10cc9d21ba3d2df2a50e to your computer and use it in GitHub Desktop.
Save alexi190/10cc9d21ba3d2df2a50e to your computer and use it in GitHub Desktop.
Cake Rest Api Test
Added in route.php
$routes->connect('/', ['controller' => 'Users', 'action' => 'index', 'home']);
$routes->connect('/users/*', ['controller' => 'Users', 'action' => 'index']);
Router::scope('/', function ($routes) {
$routes->extensions(['xml']);
$routes->resources('Users');
});
And create a file in /Template/Users/xml/index.ctp
$xml = Xml::fromArray(['response' => $users]);
echo $xml->asXML();
UsersController.php
class UsersController extends AppController
{
/**
* Index method
*
* @return void
*/
public function index()
{
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);
}
/**
* View method
*
* @param string|null $id User id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
/**
* Add method
*
* @return void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Edit method
*
* @param string|null $id User id.
* @return void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Delete method
*
* @param string|null $id User id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
If I hit webservice from a rest client
http://localhost:8888/MyProject/users/index.json
I am not getting xml.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment