Skip to content

Instantly share code, notes, and snippets.

@RyanDawkins
Created August 7, 2014 03:44
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 RyanDawkins/af1c5365579f8824b80c to your computer and use it in GitHub Desktop.
Save RyanDawkins/af1c5365579f8824b80c to your computer and use it in GitHub Desktop.
Parent/Child REST Controller for CRUD
// Routing
'api/:parent/:pk/:child/create' => 'api/parent/child',
// controller/api/parent.php
<?php
class Controller_Api_Parent extends Controller_Crud
{
public function get_child()
{
$parent = "Model_Api_Orm_".ucfirst($this->param("parent"));
$child = "Model_Api_Orm_".ucfirst($this->param("child"));
$parent_model = new $parent();
$parent_orm = $parent_model::find($this->param("pk"));
$child_model = new $child();
$child_orm = $child_model->from_array(Input::json());
$child_orm->save();
return $this->response(array(
"parent" => array(
$parent => $parent_orm->to_array(),
),
"child" => array(
$child => $child_orm->to_array(),
),
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment