diff --git a/doc/options.md b/doc/options.md index cf38043..72a95e9 100644 --- a/doc/options.md +++ b/doc/options.md @@ -75,4 +75,33 @@ | `--fields` | The field names for the form. e.g. ```--fields='title#string; content#text``` | | `--locales` | Locales language type. e.g. locals=en | +### API CRUD Options: + +| Option | Description | +| --- | --- | +| `--fields` | The field names for the form. e.g. ```--fields='title#string; content#text; category#select#options={"technology": "Technology", "tips": "Tips", "health": "Health"}; user_id#integer#unsigned'``` | +| `--fields_from_file` | Fields from a JSON file. e.g. `--fields_from_file="/path/to/fields.json"` | +| `--validations` | Validation rules for the fields "col_name#rules_set" e.g. ` "title#min:10|max:30|required" ` - See https://laravel.com/docs/master/validation#available-validation-rules | +| `--controller-namespace` | The namespace of the controller - sub directories will be created | +| `--model-namespace` | The namespace that the model will be placed in - directories will be created | +| `--pk` | The name of the primary key | +| `--pagination` | The amount of models per page for index pages | +| `--indexes` | The fields to add an index to. append "#unique" to a field name to add a unique index. Create composite fields by separating fieldnames with a pipe (` --indexes="title,field1|field2#unique" ` will create normal index on title, and unique composite on fld1 and fld2) | +| `--foreign-keys` | Any foreign keys for the table. e.g. `--foreign-keys="user_id#id#users#cascade"` where user_id is the column name, id is the name of the field on the foreign table, users is the name of the foreign table, and cascade is the operation 'ON DELETE' together with 'ON UPDATE' | +| `--relationships` | The relationships for the model. e.g. `--relationships="comments#hasMany#App\Comment"` in the format | +| `--route` | Include Crud route to routes.php? yes or no | +| `--route-group` | Prefix of the route group | + +### API Controller Options: + +| Option | Description | +| --- | --- | +| `--crud-name` | The name of the crud. e.g. ```--crud-name="post"``` | +| `--model-name` | The name of the model. e.g. ```--model-name="Post"``` | +| `--model-namespace` | The namespace of the model. e.g. ```--model-namespace="Custom\Namespace\Post"``` | +| `--controller-namespace` | The namespace of the controller. e.g. ```--controller-namespace="Http\Controllers\Client"``` | +| `--validations` | Validation rules for the fields "col_name#rules_set" e.g. ``` "title#min:10|max:30|required" ``` - See https://laravel.com/docs/master/validation#available-validation-rules | +| `--pagination` | The amount of models per page for index pages | +| `--force` | Overwrite already existing controller. | + [← Back to index](README.md) diff --git a/doc/usage.md b/doc/usage.md index c6d73a1..531314a 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -100,4 +100,18 @@ If you chose not to add the crud route in automatically (see above), you will ne Route::resource('posts', 'PostsController'); ``` +### API Commands + +For Api Crud + +``` +php artisan crud:api Posts --fields='title#string; content#text' --controller-namespace=Api +``` + +For api controller + +``` +php artisan crud:api-controller Api\\PostsController --crud-name=posts --model-name=Post +``` + [← Back to index](README.md) diff --git a/src/Commands/CrudApiCommand.php b/src/Commands/CrudApiCommand.php index 2670c56..815a859 100644 --- a/src/Commands/CrudApiCommand.php +++ b/src/Commands/CrudApiCommand.php @@ -12,7 +12,7 @@ class CrudApiCommand extends Command * * @var string */ - protected $signature = 'crud:generate + protected $signature = 'crud:api {name : The name of the Crud.} {--fields= : Field names for the form & migration.} {--fields_from_file= : Fields from a json file.} @@ -32,7 +32,7 @@ class CrudApiCommand extends Command * * @var string */ - protected $description = 'Generate Crud including controller, model, views & migrations.'; + protected $description = 'Generate api crud including controller, model & migrations.'; /** @var string */ protected $routeName = ''; @@ -105,7 +105,7 @@ public function handle() $validations = $this->processJSONValidations($this->option('fields_from_file')); } - $this->call('crud:apicontroller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--pagination' => $perPage, '--validations' => $validations]); + $this->call('crud:api-controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--pagination' => $perPage, '--validations' => $validations]); $this->call('crud:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships]); $this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys]); diff --git a/src/Commands/CrudApiControllerCommand.php b/src/Commands/CrudApiControllerCommand.php index f3c340c..d912938 100644 --- a/src/Commands/CrudApiControllerCommand.php +++ b/src/Commands/CrudApiControllerCommand.php @@ -11,7 +11,7 @@ class CrudApiControllerCommand extends GeneratorCommand * * @var string */ - protected $signature = 'crud:apicontroller + protected $signature = 'crud:api-controller {name : The name of the controler.} {--crud-name= : The name of the Crud.} {--model-name= : The name of the Model.} diff --git a/src/CrudGeneratorServiceProvider.php b/src/CrudGeneratorServiceProvider.php index 149b44d..6eb8411 100644 --- a/src/CrudGeneratorServiceProvider.php +++ b/src/CrudGeneratorServiceProvider.php @@ -54,7 +54,7 @@ public function register() 'Appzcoder\CrudGenerator\Commands\CrudViewCommand', 'Appzcoder\CrudGenerator\Commands\CrudLangCommand', 'Appzcoder\CrudGenerator\Commands\CrudApiCommand', - 'Appzcoder\CrudGenerator\Commands\CrudApiControllerCommand', + 'Appzcoder\CrudGenerator\Commands\CrudApiControllerCommand' ); } } diff --git a/src/stubs/api-controller.stub b/src/stubs/api-controller.stub index 67e3afb..3378584 100644 --- a/src/stubs/api-controller.stub +++ b/src/stubs/api-controller.stub @@ -19,7 +19,7 @@ class DummyClass extends Controller { ${{crudName}} = {{modelName}}::paginate({{pagination}}); - return ${{crudName}} + return ${{crudName}}; } /** @@ -34,7 +34,7 @@ class DummyClass extends Controller {{validationRules}} ${{crudNameSingular}} = {{modelName}}::create($request->all()); - return ${{crudNameSingular}}; + return response()->json(${{crudNameSingular}}, 201); } /** @@ -65,7 +65,7 @@ class DummyClass extends Controller ${{crudNameSingular}} = {{modelName}}::findOrFail($id); ${{crudNameSingular}}->update($request->all()); - return ${{crudNameSingular}}; + return response()->json(${{crudNameSingular}}, 200); } /** @@ -79,6 +79,6 @@ class DummyClass extends Controller { {{modelName}}::destroy($id); - return 204; + return response()->json(null, 204); } }