I'm trying to build a RESTful api for multiple models.
I found this:
class RecipesController extends AppController {
public $components = array('RequestHandler');
public function index() {
$recipes = $this->Recipe->find('all');
$this->set(array(
'recipes' => $recipes,
'_serialize' => array('recipes')
));
}
public function view($id) {
$recipe = $this->Recipe->findById($id);
$this->set(array(
'recipe' => $recipe,
'_serialize' => array('recipe')
));
}
public function edit($id) {
$this->Recipe->id = $id;
if ($this->Recipe->save($this->request->data)) {
$message = 'Saved';
} else {
$message = 'Error';
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
}
public function delete($id) {
if ($this->Recipe->delete($id)) {
$message = 'Deleted';
} else {
$message = 'Error';
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
}
}
But I'm trying to avoid duplicating all those methods in each controller that I need to create. I've been looking for a component that will do all the CRUD operations that I can include in each controller.
1) Does such a component exist? 2) Am I overkilling this thing when I should just duplicate this basic controller for each model?
I assume your best bet would be this:
https://github.com/FriendsOfCake/crud
"CakePHP Application development on steroids - rapid prototyping / scaffolding & production ready code - JSON APIs and more"