Last active
May 17, 2024 04:21
-
-
Save Ehesp/01c06e90d1097f11eb21 to your computer and use it in GitHub Desktop.
Angular + Laravel POST Updating
This file contains hidden or 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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Api Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles the dynamic Api request construction from an | |
| AJAX only request (determined by the X-Requested-With header). The | |
| request uses the standard Api Facade, but requires a JSON payload in | |
| the POST request to build the query. | |
| | |
*/ | |
class ApiController extends BaseController { | |
/** | |
* Time items are stored in the cache until they're removed | |
* @var int Minutes | |
*/ | |
private static $cacheTime = 5; | |
/** | |
* Decodes a given JSON object into a forced array | |
* @var array | |
*/ | |
private function parseObject( $obj ) | |
{ | |
return (array) json_decode($obj, true); | |
} | |
private function createKey($method, $path, $data) { | |
return substr(sha1($method . ':' . $path . ':' . $data), 0, 30); | |
} | |
/** | |
* Builds the Api query from the JSON payload. | |
* AJAX request only. | |
* @request POST | |
* @var json | HTTP 404 | |
*/ | |
public function buildQuery() | |
{ | |
if(Request::ajax()) { | |
$method = Input::get('method'); | |
$path = Input::get('path'); | |
$data = $this->parseObject(Input::get('data')); | |
$cacheKey = $this->createKey($method, $path, Input::get('data')); | |
if(Cache::has($cacheKey)) { | |
return Crypt::decrypt(Cache::get($cacheKey)); | |
} | |
else { | |
$query = Api::$method($path, $data); | |
Cache::put($cacheKey, Crypt::encrypt($query), self::$cacheTime); | |
return $query; | |
} | |
} | |
App::abort(404); | |
} | |
} |
This file contains hidden or 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
app.controller('CategoryCtrl', function($scope, Api) { | |
$scope.loading = true; | |
Api.query('get', 'categories') | |
.success(function(response) { | |
$scope.categories = response; | |
$scope.loading = false; | |
}); | |
}); |
This file contains hidden or 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
Route::post('api', 'ApiController@buildQuery'); |
This file contains hidden or 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
/** | |
* Api Service | |
* | |
*/ | |
app.factory('Api', function($http) { | |
return { | |
query : function( method, path, data ) { | |
if(data === undefined) | |
var data = null; | |
else | |
var data = "'" + data + "'"; | |
return $http({ | |
method: 'POST', | |
url: 'api', | |
headers: { 'Content-Type' : 'application/json', 'X-Requested-With' : 'XMLHttpRequest' }, | |
data: { 'method': method, 'path': path, 'data': data }, | |
}); | |
}, | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment