diff --git a/src/Illuminate/Foundation/Console/ModelMakeCommand.php b/src/Illuminate/Foundation/Console/ModelMakeCommand.php index df026c4de920..fc53dc6931bb 100644 --- a/src/Illuminate/Foundation/Console/ModelMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ModelMakeCommand.php @@ -133,6 +133,7 @@ protected function createController() 'name' => "{$controller}Controller", '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, '--api' => $this->option('api'), + '--requests' => $this->option('requests'), ])); } @@ -205,6 +206,7 @@ protected function getOptions() ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'], ['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'], ['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API controller'], + ['requests', 'R', InputOption::VALUE_NONE, 'Create a new FormRequest class and use it in resource controller'], ]; } } diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index 047f3adfcc05..63ac8bf7fb17 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -158,6 +158,8 @@ protected function buildModelReplacements(array $replace) } } + $replace = $this->buildFormRequestReplacements($replace, $modelClass); + return array_merge($replace, [ 'DummyFullModelClass' => $modelClass, '{{ namespacedModel }}' => $modelClass, @@ -188,6 +190,49 @@ protected function parseModel($model) return $this->qualifyModel($model); } + /** + * Build the model replacement values. + * + * @param array $replace + * @param string $modelClass + * @return array + */ + protected function buildFormRequestReplacements(array $replace, $modelClass) + { + $storeRequestClass = $updateRequestClass = 'Request'; + $namespace = 'Illuminate\\Http'; + + if ($this->option('requests')) { + $storeRequestClass = 'Store'.class_basename($modelClass).'Request'; + $this->call('make:request', [ + 'name' => $storeRequestClass, + ]); + $updateRequestClass = 'Update'.class_basename($modelClass).'Request'; + $this->call('make:request', [ + 'name' => $updateRequestClass, + ]); + $namespace = 'App\\Http\\Requests'; + } + + $namespacedRequests = $namespace.'\\'.$storeRequestClass.';'; + if ($storeRequestClass != $updateRequestClass) { + $namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';'; + } + + return array_merge($replace, [ + '{{ storeRequest }}' => $storeRequestClass, + '{{storeRequest}}' => $storeRequestClass, + '{{ updateRequest }}' => $updateRequestClass, + '{{updateRequest}}' => $updateRequestClass, + '{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass, + '{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass, + '{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass, + '{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass, + '{{ namespacedRequests }}' => $namespacedRequests, + '{{namespacedRequests}}' => $namespacedRequests, + ]); + } + /** * Get the console command options. * @@ -203,6 +248,7 @@ protected function getOptions() ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'], ['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class.'], ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'], + ['requests', 'R', InputOption::VALUE_NONE, 'Generate FormRequest classes for store and update.'], ]; } } diff --git a/src/Illuminate/Routing/Console/stubs/controller.model.api.stub b/src/Illuminate/Routing/Console/stubs/controller.model.api.stub index 0ab2f5a5fd66..4da21ed0587a 100644 --- a/src/Illuminate/Routing/Console/stubs/controller.model.api.stub +++ b/src/Illuminate/Routing/Console/stubs/controller.model.api.stub @@ -4,7 +4,7 @@ namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; -use Illuminate\Http\Request; +use {{ namespacedRequests }} class {{ class }} extends Controller { @@ -21,10 +21,10 @@ class {{ class }} extends Controller /** * Store a newly created resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \{{ namespacedStoreRequest }} $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store({{ storeRequest }} $request) { // } @@ -43,11 +43,11 @@ class {{ class }} extends Controller /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \{{ namespacedUpdateRequest }} $request * @param \{{ namespacedModel }} ${{ modelVariable }} * @return \Illuminate\Http\Response */ - public function update(Request $request, {{ model }} ${{ modelVariable }}) + public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }}) { // } diff --git a/src/Illuminate/Routing/Console/stubs/controller.model.stub b/src/Illuminate/Routing/Console/stubs/controller.model.stub index 4f0307685031..1396bd410d5c 100644 --- a/src/Illuminate/Routing/Console/stubs/controller.model.stub +++ b/src/Illuminate/Routing/Console/stubs/controller.model.stub @@ -4,7 +4,7 @@ namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; -use Illuminate\Http\Request; +use {{ namespacedRequests }} class {{ class }} extends Controller { @@ -31,10 +31,10 @@ class {{ class }} extends Controller /** * Store a newly created resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \{{ namespacedStoreRequest }} $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store({{ storeRequest }} $request) { // } @@ -64,11 +64,11 @@ class {{ class }} extends Controller /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request + * @param \{{ namespacedUpdateRequest }} $request * @param \{{ namespacedModel }} ${{ modelVariable }} * @return \Illuminate\Http\Response */ - public function update(Request $request, {{ model }} ${{ modelVariable }}) + public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }}) { // }