├── .DS_Store ├── .gitignore ├── Plugin.php ├── Readme.md ├── composer.json ├── controllers ├── ApiGeneratorController.php ├── api │ └── readme.txt └── apigeneratorcontroller │ ├── _list_toolbar.htm │ ├── _reorder_toolbar.htm │ ├── config_form.yaml │ ├── config_list.yaml │ ├── config_reorder.yaml │ ├── create.htm │ ├── index.htm │ ├── preview.htm │ ├── reorder.htm │ └── update.htm ├── helpers └── Helpers.php ├── lang └── en │ └── lang.php ├── models ├── ApiGenerator.php └── apigenerator │ ├── columns.yaml │ └── fields.yaml ├── plugin.yaml ├── routes.php ├── template ├── controller.dot ├── controller.php ├── customcontroller.dot ├── route.dot └── routes.dot └── updates ├── builder_table_create_ahmadfatoni_apigenerator_data.php └── version.yaml /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-lav/api-generator/57d193cf55270c63f7e81b8b77ece5c43746c384/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /controllers/* 2 | !/controllers/apigeneratorcontroller/* 3 | !/controllers/apigeneratorcontroller 4 | !/controllers/ApiGeneratorController.php 5 | !routes.php 6 | /controllers/API/* -------------------------------------------------------------------------------- /Plugin.php: -------------------------------------------------------------------------------- 1 | October CMS plugin to build RESTful APIs. 5 | 6 | ## Features 7 | 8 | - Auto generate routes 9 | - Auto Generate Controller (CRUD) 10 | - Support relationship restful API 11 | 12 | ## Install 13 | ``` 14 | composer require AhmadFatoni.ApiGenerator 15 | ``` 16 | 17 | ## Usage 18 | 19 | ### Form 20 | - API Name : Name of your API module 21 | - Base Endpoint : Base endpoint of your API, ex : api/v1/modulename 22 | - Short Description : Describe your API 23 | - Model : select model that will be created API 24 | - Custom Condition : Build customer response using JSON modeling 25 | 26 | ### Custom Condition Example 27 | ``` 28 | { 29 | 'fillable': 'id,title,content', 30 | 'relation': [{ 31 | 'name': 'user', 32 | 'fillable': 'id,first_name' 33 | }, { 34 | 'name': 'categories', 35 | 'fillable': 'id,name 36 | }] 37 | } 38 | ``` 39 | * please replace single quote with quote 40 | 41 | ## Contribute 42 | 43 | Pull Requests accepted. 44 | 45 | ## Contact 46 | 47 | You can communicate with me using [linkedin](https://www.linkedin.com/in/ahmad-fatoni) 48 | 49 | ## License 50 | The OctoberCMS platform is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 51 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ahmadfatoni/apigenerator-plugin", 3 | "type": "october-plugin", 4 | "description": "October CMS plugin to build RESTful APIs", 5 | "require": { 6 | "composer/installers": "~1.0", 7 | "rainlab/builder-plugin": "^2.0" 8 | } 9 | } -------------------------------------------------------------------------------- /controllers/ApiGeneratorController.php: -------------------------------------------------------------------------------- 1 | files = $files; 30 | } 31 | 32 | /** 33 | * delete selected data (multiple delete) 34 | * @return [type] [description] 35 | */ 36 | public function index_onDelete() 37 | { 38 | if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { 39 | 40 | foreach ($checkedIds as $id) { 41 | if ((!$item = ApiGenerator::find($id))) 42 | continue; 43 | $name = $item->name; 44 | if($item->delete()){ 45 | $this->deleteApi($name); 46 | } 47 | } 48 | 49 | Flash::success('Successfully deleted those data.'); 50 | } 51 | 52 | return $this->listRefresh(); 53 | } 54 | 55 | /** 56 | * generate API 57 | * @param Request $request [description] 58 | * @return [type] [description] 59 | */ 60 | public function generateApi(Request $request){ 61 | 62 | $data['model'] = $request->model; 63 | $modelname = explode("\\", $request->model); 64 | $modelname = $modelname[count($modelname)-1]; 65 | $data['modelname'] = $modelname; 66 | $data['controllername'] = str_replace(" ", "", $request->name); 67 | $data['endpoint'] = $request->endpoint; 68 | $data['custom_format'] = $request->custom_format; 69 | 70 | if( strpos($data['controllername'], ".") OR strpos($data['controllername'], "/") ){ 71 | 72 | Flash::success('Failed to create data, invalid API name.'); 73 | return Redirect::to( Backend::url($this->homePage)); 74 | 75 | } 76 | 77 | if( isset($request->id) ){ 78 | $this->deleteApi($request->oldname, 'false'); 79 | } 80 | 81 | $this->files->put(__DIR__ . $this->path . $data['controllername'].'Controller.php', $this->compile($data)); 82 | 83 | $this->files->put(__DIR__ . '/'.'../routes.php', $this->compileRoute($data)); 84 | 85 | return Redirect::to( Backend::url($this->homePage)); 86 | 87 | } 88 | 89 | /** 90 | * delete available API 91 | * @param [type] $name [description] 92 | * @param [type] $redirect [description] 93 | * @return [type] [description] 94 | */ 95 | public function deleteApi($name, $redirect = null){ 96 | 97 | $fileLocation = __DIR__ . $this->path.$name; 98 | $fileLocation = str_replace(".", "", $fileLocation); 99 | 100 | if( ! file_exists($fileLocation.'Controller.php') ){ 101 | 102 | Flash::success('Failed to delete data, invalid file location.'); 103 | return Redirect::to( Backend::url($this->homePage)); 104 | 105 | } 106 | 107 | if( strpos( strtolower($name), 'apigenerator' ) === false){ 108 | $data = []; 109 | 110 | //generate new route 111 | $this->files->put(__DIR__ . '/'.'../routes.php', $this->compileRoute($data)); 112 | 113 | //remove controller 114 | if (file_exists( __DIR__ . $this->path.$name.'Controller.php' )) { 115 | 116 | unlink(__DIR__ . $this->path.$name.'Controller.php'); 117 | 118 | } 119 | 120 | if( $redirect != null ){ 121 | return 'success without redirect'; 122 | } 123 | } 124 | 125 | return Redirect::to( Backend::url($this->homePage)); 126 | 127 | } 128 | 129 | public function updateApi($name){ 130 | 131 | } 132 | 133 | /** 134 | * compile controller from template 135 | * @param [type] $data [description] 136 | * @return [type] [description] 137 | */ 138 | public function compile($data){ 139 | if( $data['custom_format'] != ''){ 140 | 141 | $template = $this->files->get(__DIR__ .'/../template/customcontroller.dot'); 142 | $template = $this->replaceAttribute($template, $data); 143 | $template = $this->replaceCustomAttribute($template, $data); 144 | }else{ 145 | $template = $this->files->get(__DIR__ .'/../template/controller.dot'); 146 | $template = $this->replaceAttribute($template, $data); 147 | } 148 | return $template; 149 | } 150 | 151 | /** 152 | * replace attribute 153 | * @param [type] $template [description] 154 | * @param [type] $data [description] 155 | * @return [type] [description] 156 | */ 157 | public function replaceAttribute($template, $data){ 158 | if( isset( $data['model'] ) ){ 159 | $template = str_replace('{{model}}', $data['model'], $template); 160 | } 161 | $template = str_replace('{{modelname}}', $data['modelname'], $template); 162 | $template = str_replace('{{controllername}}', $data['controllername'], $template); 163 | return $template; 164 | } 165 | 166 | /** 167 | * replace custom attribute 168 | * @param [type] $template [description] 169 | * @param [type] $data [description] 170 | * @return [type] [description] 171 | */ 172 | public function replaceCustomAttribute($template, $data){ 173 | 174 | $arr = str_replace('\t', '', $data['custom_format']); 175 | $arr = json_decode($arr); 176 | $select = str_replace('
', '', $this->compileOpenIndexFunction($data['modelname'], 'index')); 177 | $show = str_replace('
', '', $this->compileOpenIndexFunction($data['modelname'], 'show')); 178 | $fillableParent = ''; 179 | 180 | if( isset($arr->fillable) AND $arr->fillable != null ) { 181 | $fillableParent = $this->compileFillableParent($arr->fillable); 182 | } 183 | 184 | if( isset($arr->relation) AND $arr->relation != null AND is_array($arr->relation) AND count($arr->relation) > 0) { 185 | $select .= str_replace('
', '', $this->compileFillableChild($arr->relation)); 186 | $show .= str_replace('
', '', $this->compileFillableChild($arr->relation)); 187 | } 188 | 189 | $select .= "->select(".$fillableParent.")"; 190 | $show .= "->select(".$fillableParent.")->where('id', '=', \$id)->first();"; 191 | 192 | ( $fillableParent != '') ? $select .= "->get()->toArray();" : $select .= "->toArray();" ; 193 | 194 | $closeFunction = str_replace('
', '', nl2br( 195 | " 196 | return \$this->helpers->apiArrayResponseBuilder(200, 'success', \$data); 197 | }")); 198 | $select .= $closeFunction; 199 | $show .= $closeFunction; 200 | 201 | $template = str_replace('{{select}}', $select, $template); 202 | $template = str_replace('{{show}}', $show, $template); 203 | 204 | return $template; 205 | } 206 | 207 | public function compileOpenIndexFunction($modelname, $type){ 208 | if( $type == 'index'){ 209 | return nl2br(" 210 | public function index(){ 211 | \$data = \$this->".$modelname); 212 | }else{ 213 | return nl2br(" 214 | public function show(\$id){ 215 | \$data = \$this->".$modelname); 216 | } 217 | 218 | } 219 | 220 | public function compileFillableParent($fillable){ 221 | 222 | $fillableParentArr = explode(",", $fillable); 223 | $fillableParent = ''; 224 | 225 | foreach ($fillableParentArr as $key) { 226 | 227 | $fillableParent .= ",'".$key."'"; 228 | 229 | } 230 | 231 | $fillableParent = substr_replace($fillableParent, '', 0 , 1); 232 | 233 | return $fillableParent; 234 | } 235 | 236 | public function compileFillableChild($fillable){ 237 | 238 | $select = "->with(array("; 239 | 240 | foreach ($fillable as $key) { 241 | 242 | $fillableChild = ""; 243 | 244 | if( isset($key->fillable) AND $key->fillable != null ){ 245 | $fillableChildArr = explode(",", $key->fillable); 246 | 247 | 248 | foreach ($fillableChildArr as $key2) { 249 | 250 | $fillableChild .= ",'".$key2."'"; 251 | 252 | } 253 | 254 | $fillableChild = substr_replace($fillableChild, '', 0 , 1); 255 | } 256 | 257 | $select .= nl2br( 258 | " 259 | '".$key->name."'=>function(\$query){ 260 | \$query->select(".$fillableChild."); 261 | },"); 262 | 263 | } 264 | 265 | $select .= " ))"; 266 | 267 | return $select; 268 | } 269 | 270 | public function compileRoute($data){ 271 | 272 | $oldData = ApiGenerator::all(); 273 | $routeList = ""; 274 | 275 | if( count($oldData) > 0 ){ 276 | 277 | $routeList .= $this->parseRouteOldData($oldData, $data); 278 | 279 | } 280 | 281 | if( count($data) > 0 ){ 282 | $data['modelname'] = $data['endpoint']; 283 | if( $data['modelname'][0] == "/" ){ 284 | $data['modelname'] = substr_replace($data['modelname'], '', 0 , 1); 285 | } 286 | $routeList .= $this->parseRoute($data); 287 | } 288 | 289 | $route = $this->files->get(__DIR__ .'/../template/routes.dot'); 290 | $route = str_replace('{{route}}', $routeList, $route); 291 | 292 | return $route; 293 | 294 | } 295 | 296 | public function parseRouteOldData($oldData, $data = null){ 297 | 298 | $routeList = ""; 299 | 300 | if( count($data) == 0 ) $data['modelname']=''; 301 | 302 | foreach ( $oldData as $key ) { 303 | 304 | $modelname = explode("\\", $key->model); 305 | $modelname = $modelname[count($modelname)-1]; 306 | $old['modelname'] = $key->endpoint; 307 | $old['controllername'] = $key->name; 308 | 309 | if( $data['modelname'] != $modelname ){ 310 | 311 | if( $old['modelname'][0] == "/" ){ 312 | $old['modelname'] = substr_replace($old['modelname'], '', 0 , 1); 313 | } 314 | 315 | $routeList .= $this->parseRoute($old); 316 | } 317 | } 318 | 319 | return $routeList; 320 | 321 | } 322 | 323 | public function parseRoute($data){ 324 | 325 | $template = $this->files->get(__DIR__ .'/../template/route.dot'); 326 | $template = $this->replaceAttribute($template, $data); 327 | return $template; 328 | } 329 | 330 | 331 | public static function getAfterFilters() {return [];} 332 | public static function getBeforeFilters() {return [];} 333 | public function callAction($method, $parameters=false) { 334 | return call_user_func_array(array($this, $method), $parameters); 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /controllers/api/readme.txt: -------------------------------------------------------------------------------- 1 | api controller here -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/_list_toolbar.htm: -------------------------------------------------------------------------------- 1 |
2 | 3 | 18 |
19 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/_reorder_toolbar.htm: -------------------------------------------------------------------------------- 1 |
2 | 3 |
-------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/config_form.yaml: -------------------------------------------------------------------------------- 1 | name: ApiGeneratorController 2 | form: $/ahmadfatoni/apigenerator/models/apigenerator/fields.yaml 3 | modelClass: AhmadFatoni\ApiGenerator\Models\ApiGenerator 4 | defaultRedirect: ahmadfatoni/apigenerator/apigeneratorcontroller 5 | create: 6 | redirect: 'ahmadfatoni/apigenerator/apigeneratorcontroller/update/:id' 7 | redirectClose: ahmadfatoni/apigenerator/apigeneratorcontroller 8 | update: 9 | redirect: ahmadfatoni/apigenerator/apigeneratorcontroller 10 | redirectClose: ahmadfatoni/apigenerator/apigeneratorcontroller 11 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/config_list.yaml: -------------------------------------------------------------------------------- 1 | list: $/ahmadfatoni/apigenerator/models/apigenerator/columns.yaml 2 | modelClass: AhmadFatoni\ApiGenerator\Models\ApiGenerator 3 | title: ApiGeneratorController 4 | noRecordsMessage: 'backend::lang.list.no_records' 5 | showSetup: true 6 | showCheckboxes: true 7 | toolbar: 8 | buttons: list_toolbar 9 | search: 10 | prompt: 'backend::lang.list.search_prompt' 11 | recordUrl: 'ahmadfatoni/apigenerator/apigeneratorcontroller/update/:id' 12 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/config_reorder.yaml: -------------------------------------------------------------------------------- 1 | title: ApiGeneratorController 2 | modelClass: AhmadFatoni\ApiGenerator\Models\ApiGenerator 3 | toolbar: 4 | buttons: reorder_toolbar 5 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/create.htm: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | fatalError): ?> 9 | 10 | 'layout']) ?> 11 | 12 |
13 | formRender() ?> 14 |
15 | 16 | 45 | 46 |
47 |
48 | 57 | 58 | 59 | 62 | 63 | 64 | Cancel 65 | 66 |
67 |
68 | 69 | 70 | 71 | 82 | 83 | 84 |

fatalError)) ?>

85 |

86 | 87 | 88 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/index.htm: -------------------------------------------------------------------------------- 1 | listRender() ?> 2 | -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/preview.htm: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | fatalError): ?> 9 | 10 |
11 | formRenderPreview() ?> 12 |
13 | 14 | 15 |

fatalError) ?>

16 | 17 | 18 |

19 | 20 | 21 | 22 |

-------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/reorder.htm: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | reorderRender() ?> -------------------------------------------------------------------------------- /controllers/apigeneratorcontroller/update.htm: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | fatalError): ?> 9 | 10 | 'layout']) ?> 11 | 12 |
13 | formRender() ?> 14 |
15 | 16 | 45 |
46 |
47 | 57 | 58 |
Cancel
59 | 60 | 68 | 69 | 70 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 |

fatalError)) ?>

81 |

82 | 83 | 84 | 91 | 92 | 109 | 115 | 116 | -------------------------------------------------------------------------------- /helpers/Helpers.php: -------------------------------------------------------------------------------- 1 | (isset($statusCode)) ? $statusCode : 500, 9 | 'message' => (isset($message)) ? $message : 'error' 10 | ]; 11 | 12 | if ( is_array($data) && count($data) > 0) { 13 | $arr['data'] = $data; 14 | } 15 | 16 | return response()->json($arr, $arr['status_code']); 17 | //return $arr; 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /lang/en/lang.php: -------------------------------------------------------------------------------- 1 | [ 3 | 'name' => 'API-Generator', 4 | 'description' => 'Generate API base on Builder Plugin' 5 | ] 6 | ]; -------------------------------------------------------------------------------- /models/ApiGenerator.php: -------------------------------------------------------------------------------- 1 | 'required|unique:ahmadfatoni_apigenerator_data,name|regex:/^[\pL\s\-]+$/u', 18 | 'endpoint' => 'required|unique:ahmadfatoni_apigenerator_data,endpoint', 19 | 'custom_format' => 'json' 20 | ]; 21 | 22 | public $customMessages = [ 23 | 'custom_format.json' => 'Invalid Json Format Custom Condition' 24 | ]; 25 | 26 | /* 27 | * Disable timestamps by default. 28 | * Remove this line if timestamps are defined in the database table. 29 | */ 30 | public $timestamps = false; 31 | 32 | /** 33 | * @var string The database table used by the model. 34 | */ 35 | public $table = 'ahmadfatoni_apigenerator_data'; 36 | 37 | /** 38 | * get model List 39 | * @return [type] [description] 40 | */ 41 | public function getModelOptions(){ 42 | 43 | return ComponentHelper::instance()->listGlobalModels(); 44 | } 45 | 46 | /** 47 | * [setCustomFormatAttribute description] 48 | * @param [type] $value [description] 49 | */ 50 | public function setCustomFormatAttribute($value){ 51 | 52 | $json = str_replace('\t', '', $value); 53 | $json = json_decode($json); 54 | 55 | if( $json != null){ 56 | 57 | if( ! isset($json->fillable) AND ! isset($json->relation) ){ 58 | 59 | return $this->attributes['custom_format'] = 'invalid format'; 60 | 61 | } 62 | 63 | if( isset($json->relation) AND $json->relation != null ){ 64 | foreach ($json->relation as $key) { 65 | if( !isset($key->name) OR $key->name == null ){ 66 | return $this->attributes['custom_format'] = 'invalid format'; 67 | } 68 | } 69 | } 70 | } 71 | 72 | return $this->attributes['custom_format'] = $value; 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /models/apigenerator/columns.yaml: -------------------------------------------------------------------------------- 1 | columns: 2 | name: 3 | label: 'API NAME' 4 | type: text 5 | searchable: true 6 | sortable: true 7 | endpoint: 8 | label: 'BASE ENDPOINT' 9 | type: text 10 | -------------------------------------------------------------------------------- /models/apigenerator/fields.yaml: -------------------------------------------------------------------------------- 1 | fields: 2 | name: 3 | label: 'API Name' 4 | oc.commentPosition: '' 5 | span: auto 6 | placeholder: 'Name of your API' 7 | required: 1 8 | type: text 9 | endpoint: 10 | label: 'Base Endpoint' 11 | oc.commentPosition: '' 12 | span: auto 13 | placeholder: api/v1/modulename 14 | required: 1 15 | type: text 16 | description: 17 | label: 'Short Description' 18 | oc.commentPosition: '' 19 | span: auto 20 | placeholder: 'Descript your API' 21 | type: text 22 | model: 23 | label: 'Select Model' 24 | oc.commentPosition: '' 25 | span: auto 26 | required: 1 27 | type: dropdown 28 | custom_format: 29 | label: 'Custom Condition (Fillable and Relation)' 30 | size: large 31 | oc.commentPosition: '' 32 | span: full 33 | type: textarea 34 | -------------------------------------------------------------------------------- /plugin.yaml: -------------------------------------------------------------------------------- 1 | plugin: 2 | name: 'ahmadfatoni.apigenerator::lang.plugin.name' 3 | description: 'ahmadfatoni.apigenerator::lang.plugin.description' 4 | author: AhmadFatoni 5 | icon: oc-icon-bolt 6 | homepage: '' 7 | navigation: 8 | api-generator: 9 | label: 'API Generator' 10 | url: ahmadfatoni/apigenerator/apigeneratorcontroller 11 | icon: icon-cogs 12 | permissions: 13 | - ahmadfatoni.apigenerator.manage 14 | permissions: 15 | ahmadfatoni.apigenerator.manage: 16 | tab: 'API Generator' 17 | label: 'Manage the API Generator' 18 | -------------------------------------------------------------------------------- /routes.php: -------------------------------------------------------------------------------- 1 | 'fatoni.generate.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@generateApi')); 4 | Route::post('fatoni/update/api/{id}', array('as' => 'fatoni.update.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@updateApi')); 5 | Route::get('fatoni/delete/api/{id}', array('as' => 'fatoni.delete.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@deleteApi')); 6 | 7 | Route::resource('halo', 'AhmadFatoni\ApiGenerator\Controllers\API\haloController', ['except' => ['destroy', 'create', 'edit']]); 8 | Route::get('halo/{id}/delete', ['as' => 'halo.delete', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\API\haloController@destroy']); -------------------------------------------------------------------------------- /template/controller.dot: -------------------------------------------------------------------------------- 1 | {{modelname}} = ${{modelname}}; 21 | $this->helpers = $helpers; 22 | } 23 | 24 | public function index(){ 25 | 26 | $data = $this->{{modelname}}->all()->toArray(); 27 | 28 | return $this->helpers->apiArrayResponseBuilder(200, 'success', $data); 29 | } 30 | 31 | public function show($id){ 32 | 33 | $data = $this->{{modelname}}::find($id); 34 | 35 | if ($data){ 36 | return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]); 37 | } else { 38 | $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']); 39 | } 40 | 41 | } 42 | 43 | public function store(Request $request){ 44 | 45 | $arr = $request->all(); 46 | 47 | while ( $data = current($arr)) { 48 | $this->{{modelname}}->{key($arr)} = $data; 49 | next($arr); 50 | } 51 | 52 | $validation = Validator::make($request->all(), $this->{{modelname}}->rules); 53 | 54 | if( $validation->passes() ){ 55 | $this->{{modelname}}->save(); 56 | return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->{{modelname}}->id]); 57 | }else{ 58 | return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() ); 59 | } 60 | 61 | } 62 | 63 | public function update($id, Request $request){ 64 | 65 | $status = $this->{{modelname}}->where('id',$id)->update($request->all()); 66 | 67 | if( $status ){ 68 | 69 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.'); 70 | 71 | }else{ 72 | 73 | return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.'); 74 | 75 | } 76 | } 77 | 78 | public function delete($id){ 79 | 80 | $this->{{modelname}}->where('id',$id)->delete(); 81 | 82 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.'); 83 | } 84 | 85 | public function destroy($id){ 86 | 87 | $this->{{modelname}}->where('id',$id)->delete(); 88 | 89 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.'); 90 | } 91 | 92 | public static function getAfterFilters() 93 | { 94 | return []; 95 | } 96 | 97 | public static function getBeforeFilters() 98 | { 99 | return []; 100 | } 101 | 102 | public static function getMiddleware() 103 | { 104 | return []; 105 | } 106 | 107 | /** 108 | * Execute an action on the controller. 109 | * 110 | * @param string $method 111 | * @param array $parameters 112 | * @return \Symfony\Component\HttpFoundation\Response 113 | */ 114 | public function callAction($method, $parameters) 115 | { 116 | return $this->{$method}(...array_values($parameters)); 117 | } 118 | 119 | } -------------------------------------------------------------------------------- /template/controller.php: -------------------------------------------------------------------------------- 1 | {{modelname}} = ${{modelname}}; 16 | } 17 | 18 | public static function getAfterFilters() {return [];} 19 | public static function getBeforeFilters() {return [];} 20 | public static function getMiddleware() {return [];} 21 | public function callAction($method, $parameters=false) { 22 | return call_user_func_array(array($this, $method), $parameters); 23 | } 24 | 25 | // public function create(Request $request){ 26 | 27 | // $arr = $request->all(); 28 | 29 | // while ( $data = current($arr)) { 30 | // $this-> 31 | // } 32 | // return json_encode($this->{{modelname}}->store($request)); 33 | 34 | // } 35 | } 36 | -------------------------------------------------------------------------------- /template/customcontroller.dot: -------------------------------------------------------------------------------- 1 | {{modelname}} = ${{modelname}}; 19 | $this->helpers = $helpers; 20 | } 21 | 22 | {{select}} 23 | 24 | {{show}} 25 | 26 | public function store(Request $request){ 27 | 28 | $arr = $request->all(); 29 | 30 | while ( $data = current($arr)) { 31 | $this->{{modelname}}->{key($arr)} = $data; 32 | next($arr); 33 | } 34 | 35 | $validation = Validator::make($request->all(), $this->{{modelname}}->rules); 36 | 37 | if( $validation->passes() ){ 38 | $this->{{modelname}}->save(); 39 | return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->{{modelname}}->id]); 40 | }else{ 41 | return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() ); 42 | } 43 | 44 | } 45 | 46 | public function update($id, Request $request){ 47 | 48 | $status = $this->{{modelname}}->where('id',$id)->update($request->all()); 49 | 50 | if( $status ){ 51 | 52 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.'); 53 | 54 | }else{ 55 | 56 | return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.'); 57 | 58 | } 59 | } 60 | 61 | public function delete($id){ 62 | 63 | $this->{{modelname}}->where('id',$id)->delete(); 64 | 65 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.'); 66 | } 67 | 68 | public function destroy($id){ 69 | 70 | $this->{{modelname}}->where('id',$id)->delete(); 71 | 72 | return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.'); 73 | } 74 | 75 | public static function getAfterFilters() 76 | { 77 | return []; 78 | } 79 | 80 | public static function getBeforeFilters() 81 | { 82 | return []; 83 | } 84 | 85 | public static function getMiddleware() 86 | { 87 | return []; 88 | } 89 | 90 | /** 91 | * Execute an action on the controller. 92 | * 93 | * @param string $method 94 | * @param array $parameters 95 | * @return \Symfony\Component\HttpFoundation\Response 96 | */ 97 | public function callAction($method, $parameters) 98 | { 99 | return $this->{$method}(...array_values($parameters)); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /template/route.dot: -------------------------------------------------------------------------------- 1 | 2 | Route::resource('{{modelname}}', 'AhmadFatoni\ApiGenerator\Controllers\API\{{controllername}}Controller', ['except' => ['destroy', 'create', 'edit']]); 3 | Route::get('{{modelname}}/{id}/delete', ['as' => '{{modelname}}.delete', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\API\{{controllername}}Controller@destroy']); -------------------------------------------------------------------------------- /template/routes.dot: -------------------------------------------------------------------------------- 1 | 'fatoni.generate.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@generateApi')); 4 | Route::post('fatoni/update/api/{id}', array('as' => 'fatoni.update.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@updateApi')); 5 | Route::get('fatoni/delete/api/{id}', array('as' => 'fatoni.delete.api', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\ApiGeneratorController@deleteApi')); 6 | {{route}} -------------------------------------------------------------------------------- /updates/builder_table_create_ahmadfatoni_apigenerator_data.php: -------------------------------------------------------------------------------- 1 | engine = 'InnoDB'; 13 | $table->increments('id'); 14 | $table->string('name'); 15 | $table->string('endpoint'); 16 | $table->string('model'); 17 | $table->string('description')->nullable(); 18 | $table->text('custom_format')->nullable(); 19 | }); 20 | } 21 | 22 | public function down() 23 | { 24 | Schema::dropIfExists('ahmadfatoni_apigenerator_data'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /updates/version.yaml: -------------------------------------------------------------------------------- 1 | 1.0.1: 2 | - 'Initialize plugin.' 3 | 1.0.2: 4 | - 'Database implementation' 5 | 1.0.3: 6 | - 'add builder plugin on requirements dependency' 7 | - builder_table_create_ahmadfatoni_apigenerator_data.php 8 | 1.0.4: 9 | - 'fixing bug on PHP 7' 10 | 1.0.5: 11 | - 'fixing bug on request delete data' 12 | 1.0.6: 13 | - 'fixing bug on generate endpoint' 14 | 1.0.7: 15 | - 'fixing bug on October CMS v1.0.456' 16 | 1.0.8: 17 | - 'fixing bug on October CMS v3' --------------------------------------------------------------------------------