Last active
December 19, 2015 21:18
-
-
Save agborkowski/6018927 to your computer and use it in GitHub Desktop.
#li3 #rest #media #router #angular #js #ngResource #lithiumphp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//route.php | |
/** | |
* Lithium REST routes | |
* Support: | |
* - Angular, NgResource, $http | |
* - ExtJs (Media.php should be included) | |
* | |
* @author AgBorkowski <[email protected]> http://blog.aeonmedia.eu | |
* @see http://book.cakephp.org/view/1239/The-Simple-Setup | |
* @see http://www.sencha.com/learn/Manual:RESTful_Web_Services#HTTP_Status_Codes | |
* @see http://docs.angularjs.org/api/ngResource/service/$resource | |
* | |
* [ Method ]-----[ URL ]--------- [ Controller action invoked ] | |
* GET......./recipes..............RecipesController::index() | |
* GET......./recipes/index........RecipesController::index() | |
* | |
* GET......./recipes/123..........RecipesController::view(123) | |
* | |
* POST....../recipes..............RecipesController::add() | |
* POST....../recipes/add..........RecipesController::add() | |
* | |
* PUT......./recipes..............RecipesController::edit() | |
* PUT......./recipes/123..........RecipesController::edit(123) | |
* POST....../recipes/123/edit.....RecipesController::edit(123) | |
* POST....../recipes/123..........RecipesController::edit(123) | |
* | |
* DELETE..../recipes/123..........RecipesController::delete(123) | |
* POST....../recipes/123..........RecipesController::edit(123) | |
*/ | |
Router::connect('/{:controller}(.{:type:\w+})*', array('http:method' => 'GET', 'action' => 'index')); | |
Router::connect('/{:controller}/index(.{:type:\w+})*', array('http:method' => 'GET', 'action' => 'index')); | |
Router::connect('/{:controller}/{:id:[0-9a-f]{24}|[0-9]+}(.{:type:\w+})*', array('http:method' => 'GET', 'action' => 'view')); | |
Router::connect('/{:controller}(.{:type:\w+})*', array('http:method' => 'POST', 'action' => 'add')); | |
Router::connect('/{:controller}/add(.{:type:\w+})*', array('http:method' => 'POST', 'action' => 'add')); | |
Router::connect('/{:controller}(.{:type:\w+})*', array('http:method' => 'PUT', 'action' => 'edit')); | |
Router::connect('/{:controller}/{:id:[0-9a-f]{24}|[0-9]+}(.{:type:\w+})*', array('http:method' => 'PUT', 'action' => 'edit')); | |
Router::connect('/{:controller}/{:id:[0-9a-f]{24}|[0-9]+}(.{:type:\w+})*/edit', array('http:method' => 'POST', 'action' => 'edit')); | |
Router::connect('/{:controller}/{:id:[0-9a-f]{24}|[0-9]+}', array('http:method' => 'POST', 'action' => 'edit')); | |
Router::connect('/{:controller}/{:id:[0-9a-f]{24}|[0-9]+}(.{:type:\w+})*', array('http:method' => 'DELETE', 'action' => 'delete')); | |
/** | |
* file: Media.php | |
* Media pack response in Extjs compatibile array, skip thi for Angular. | |
**/ | |
Media::type('json', array('application/json', 'application/jsonrequest'), array( | |
'view' => 'lithium\template\View', | |
'layout' => false, | |
'conditions' => array('ajax' => true), | |
'encode' => function($data, $handler, &$response) { | |
$tmp = $data; | |
$data = array( | |
'success' => true, | |
'message' => false, | |
'data' => array(), | |
'errors' => array(), | |
'redirect' => isset($tmp['redirect']) ? $tmp['redirect'] : false | |
); | |
if($handler['controller'] === '_errors'){ | |
$data['success'] = false; | |
$data['message'][] = "({$tmp['code']}#{$tmp['id']}) {$tmp['message']}"; | |
} | |
// $flash = Flash::read(); | |
// if($flash['message']){ | |
// $data['message'][] = $flash['message']; | |
// Flash::clear(); | |
// } | |
if(!empty($tmp['message'])){ | |
$data['message'][] = $tmp['message']; | |
} | |
if(is_array($data['message'])){ | |
$data['message'] = implode(', ', $data['message']); | |
} | |
if ($data['success']) { | |
if(isset($tmp['success'])){ | |
$data['success'] = $tmp['success']; | |
} | |
if (isset($tmp['data']) && !is_string(key($tmp['data']))) { | |
// reorder array key index (from 0 to n) | |
$data['data'] = array_values($tmp['data']); | |
} elseif (isset($tmp['data'])) { | |
$data['data'] = $tmp['data']; | |
} | |
} | |
if (isset($tmp['errors'])) { | |
if(is_array($tmp['errors'])){ | |
extract(Message::aliases()); | |
foreach ($tmp['errors'] as $id => $val) { | |
if (is_array($val)) { | |
$data['errors'][$id] = ''; | |
foreach($val as $string) { | |
$data['errors'][$id] .= $t($string) . ' '; | |
} | |
}else{ | |
$data['errors'][] = array($id => $t($val)); | |
} | |
} | |
}else{ | |
$data['errors'][] = $tmp['errors']; | |
} | |
} | |
return (array) json_encode($data); | |
}, | |
'decode' => function($data, $handler) { | |
return (array) json_decode($data, true); | |
} | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment