Skip to content

Commit 996a09a

Browse files
authored
Merge pull request #204 from Laravel-Backpack/add-option-to-create-forms-without-id-in-route
add option to generate forms without id, use named parameters in formAction
2 parents aa90e14 + 4559b5e commit 996a09a

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/Console/Commands/CrudFormOperationBackpackCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CrudFormOperationBackpackCommand extends GeneratorCommand
1919
*
2020
* @var string
2121
*/
22-
protected $signature = 'backpack:crud-form-operation {name}';
22+
protected $signature = 'backpack:crud-form-operation {name} {--no-id}';
2323

2424
/**
2525
* The console command description.
@@ -71,12 +71,8 @@ protected function getDefaultNamespace($rootNamespace)
7171

7272
/**
7373
* Replace the table name for the given stub.
74-
*
75-
* @param string $stub
76-
* @param string $name
77-
* @return string
7874
*/
79-
protected function replaceNameStrings(&$stub, $name)
75+
protected function replaceNameStrings(string &$stub, string $name): self
8076
{
8177
$name = Str::of($name)->afterLast('\\');
8278

@@ -85,6 +81,11 @@ protected function replaceNameStrings(&$stub, $name)
8581
$stub = str_replace('Dummy Class', $name->snake()->replace('_', ' ')->title(), $stub);
8682
$stub = str_replace('dummy-class', $name->snake('-'), $stub);
8783
$stub = str_replace('dummy_class', $name->snake(), $stub);
84+
$stub = str_replace('DUMMY_ROUTE_WITH_ID', $this->option('no-id') ? 'false' : 'true', $stub);
85+
$stub = str_replace('DUMMY_BUTTON_STACK', $this->option('no-id') ? 'top' : 'line', $stub);
86+
$stub = str_replace('DUMMY_FORM_ACTION_CALLBACK', $this->option('no-id') ? 'formLogic: function ($inputs, $entry) {' : 'id: $id, formLogic: function ($inputs, $entry) {', $stub);
87+
$stub = str_replace('DUMMY_FUNCTION_PARAMETERS', $this->option('no-id') ? '' : 'int $id', $stub);
88+
$stub = str_replace('DUMMY_GETFORM_VIEW_PARAMETER', $this->option('no-id') ? '' : '$id', $stub);
8889

8990
return $this;
9091
}

src/Console/stubs/crud-form-operation.stub

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait DummyClassOperation
1919
{
2020
$this->formRoutes(
2121
operationName: 'dummyClass',
22-
routesHaveIdSegment: true,
22+
routesHaveIdSegment: DUMMY_ROUTE_WITH_ID,
2323
segment: $segment,
2424
routeName: $routeName,
2525
controller: $controller
@@ -33,7 +33,7 @@ trait DummyClassOperation
3333
{
3434
$this->formDefaults(
3535
operationName: 'dummyClass',
36-
// buttonStack: 'line', // alternatives: top, bottom
36+
buttonStack: 'DUMMY_BUTTON_STACK', // alternatives: top, bottom
3737
// buttonMeta: [
3838
// 'icon' => 'la la-home',
3939
// 'label' => 'Dummy Class',
@@ -48,23 +48,23 @@ trait DummyClassOperation
4848
* Method to handle the GET request and display the View with a Backpack form
4949
*
5050
*/
51-
public function getDummyClassForm(?int $id = null) : \Illuminate\Contracts\View\View
51+
public function getDummyClassForm(DUMMY_FUNCTION_PARAMETERS) : \Illuminate\Contracts\View\View
5252
{
5353
$this->crud->hasAccessOrFail('dummyClass');
5454

55-
return $this->formView($id);
55+
return $this->formView(DUMMY_GETFORM_VIEW_PARAMETER);
5656
}
5757

5858
/**
5959
* Method to handle the POST request and perform the operation
6060
*
6161
* @return array|\Illuminate\Http\RedirectResponse
6262
*/
63-
public function postDummyClassForm(?int $id = null)
63+
public function postDummyClassForm(DUMMY_FUNCTION_PARAMETERS)
6464
{
6565
$this->crud->hasAccessOrFail('dummyClass');
6666

67-
return $this->formAction($id, function ($inputs, $entry) {
67+
return $this->formAction(DUMMY_FORM_ACTION_CALLBACK
6868
// You logic goes here...
6969
// dd('got to ' . __METHOD__, $inputs, $entry);
7070

0 commit comments

Comments
 (0)