diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index c8de52c..df19527 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -39,6 +39,10 @@ class ControllerGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('form-requests')) { + $stubName .= '-formrequests'; + } + $stub = $this->getStubFileContent($stubName); $controllerFileContent = $this->replaceStubString($stub); diff --git a/src/stubs/controllers/full-formrequests.stub b/src/stubs/controllers/full-formrequests.stub new file mode 100644 index 0000000..62ba3aa --- /dev/null +++ b/src/stubs/controllers/full-formrequests.stub @@ -0,0 +1,109 @@ +where('name', 'like', '%'.request('q').'%'); + $mstrCollections = $singleMstrQuery->paginate(25); + + return view('masters.index', compact('mstrCollections')); + } + + /** + * Show the form for creating a new singleMstr. + * + * @return \Illuminate\View\View + */ + public function create() + { + $this->authorize('create', new Master); + + return view('masters.create'); + } + + /** + * Store a newly created singleMstr in storage. + * + * @param \App\Http\Requests\Masters\CreateRequest $createMasterForm + * @return \Illuminate\Routing\Redirector + */ + public function store(CreateRequest $createMasterForm) + { + $singleMstr = $createMasterForm->save(); + + return redirect()->route('masters.show', $singleMstr); + } + + /** + * Display the specified singleMstr. + * + * @param \App\Master $singleMstr + * @return \Illuminate\View\View + */ + public function show(Master $singleMstr) + { + return view('masters.show', compact('singleMstr')); + } + + /** + * Show the form for editing the specified singleMstr. + * + * @param \App\Master $singleMstr + * @return \Illuminate\View\View + */ + public function edit(Master $singleMstr) + { + $this->authorize('update', $singleMstr); + + return view('masters.edit', compact('singleMstr')); + } + + /** + * Update the specified singleMstr in storage. + * + * @param \App\Http\Requests\Masters\UpdateRequest $singleMstrUpdateForm + * @param \fullMstr $singleMstr + * @return \Illuminate\Routing\Redirector + */ + public function update(UpdateRequest $singleMstrUpdateForm, Master $singleMstr) + { + $singleMstr->update($singleMstrUpdateForm->validated()); + + return redirect()->route('masters.show', $singleMstr); + } + + /** + * Remove the specified singleMstr from storage. + * + * @param \fullMstr $singleMstr + * @return \Illuminate\Routing\Redirector + */ + public function destroy(Master $singleMstr) + { + $this->authorize('delete', $singleMstr); + + request()->validate([ + 'master_id' => 'required', + ]); + + if (request('master_id') == $singleMstr->id && $singleMstr->delete()) { + $routeParam = request()->only('page', 'q'); + + return redirect()->route('masters.index', $routeParam); + } + + return back(); + } +} diff --git a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php new file mode 100644 index 0000000..9dda61f --- /dev/null +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -0,0 +1,127 @@ +artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); + + $this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); + $ctrlClassContent = "full_model_name}; +use Illuminate\Http\Request; + +class {$this->model_name}Controller extends Controller +{ + /** + * Display a listing of the {$this->single_model_var_name}. + * + * @return \Illuminate\View\View + */ + public function index() + { + \${$this->single_model_var_name}Query = {$this->model_name}::query(); + \${$this->single_model_var_name}Query->where('name', 'like', '%'.request('q').'%'); + \${$this->collection_model_var_name} = \${$this->single_model_var_name}Query->paginate(25); + + return view('{$this->table_name}.index', compact('{$this->collection_model_var_name}')); + } + + /** + * Show the form for creating a new {$this->single_model_var_name}. + * + * @return \Illuminate\View\View + */ + public function create() + { + \$this->authorize('create', new {$this->model_name}); + + return view('{$this->table_name}.create'); + } + + /** + * Store a newly created {$this->single_model_var_name} in storage. + * + * @param \App\Http\Requests\\{$this->plural_model_name}\CreateRequest \$create{$this->model_name}Form + * @return \Illuminate\Routing\Redirector + */ + public function store(CreateRequest \$create{$this->model_name}Form) + { + \${$this->single_model_var_name} = \$create{$this->model_name}Form->save(); + + return redirect()->route('{$this->table_name}.show', \${$this->single_model_var_name}); + } + + /** + * Display the specified {$this->single_model_var_name}. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\View\View + */ + public function show({$this->model_name} \${$this->single_model_var_name}) + { + return view('{$this->table_name}.show', compact('{$this->single_model_var_name}')); + } + + /** + * Show the form for editing the specified {$this->single_model_var_name}. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\View\View + */ + public function edit({$this->model_name} \${$this->single_model_var_name}) + { + \$this->authorize('update', \${$this->single_model_var_name}); + + return view('{$this->table_name}.edit', compact('{$this->single_model_var_name}')); + } + + /** + * Update the specified {$this->single_model_var_name} in storage. + * + * @param \App\Http\Requests\\{$this->plural_model_name}\UpdateRequest \${$this->single_model_var_name}UpdateForm + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\Routing\Redirector + */ + public function update(UpdateRequest \${$this->single_model_var_name}UpdateForm, {$this->model_name} \${$this->single_model_var_name}) + { + \${$this->single_model_var_name}->update(\${$this->single_model_var_name}UpdateForm->validated()); + + return redirect()->route('{$this->table_name}.show', \${$this->single_model_var_name}); + } + + /** + * Remove the specified {$this->single_model_var_name} from storage. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\Routing\Redirector + */ + public function destroy({$this->model_name} \${$this->single_model_var_name}) + { + \$this->authorize('delete', \${$this->single_model_var_name}); + + request()->validate([ + '{$this->lang_name}_id' => 'required', + ]); + + if (request('{$this->lang_name}_id') == \${$this->single_model_var_name}->id && \${$this->single_model_var_name}->delete()) { + \$routeParam = request()->only('page', 'q'); + + return redirect()->route('{$this->table_name}.index', \$routeParam); + } + + return back(); + } +} +"; + $this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->model_name}Controller.php"))); + } +}