Skip to content

Commit

Permalink
Merge pull request #204 from Laravel-Backpack/add-option-to-create-fo…
Browse files Browse the repository at this point in the history
…rms-without-id-in-route

add option to generate forms without id, use named parameters in formAction
  • Loading branch information
pxpm authored Mar 1, 2024
2 parents aa90e14 + 4559b5e commit 996a09a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/Console/Commands/CrudFormOperationBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CrudFormOperationBackpackCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'backpack:crud-form-operation {name}';
protected $signature = 'backpack:crud-form-operation {name} {--no-id}';

/**
* The console command description.
Expand Down Expand Up @@ -71,12 +71,8 @@ protected function getDefaultNamespace($rootNamespace)

/**
* Replace the table name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceNameStrings(&$stub, $name)
protected function replaceNameStrings(string &$stub, string $name): self
{
$name = Str::of($name)->afterLast('\\');

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

return $this;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Console/stubs/crud-form-operation.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait DummyClassOperation
{
$this->formRoutes(
operationName: 'dummyClass',
routesHaveIdSegment: true,
routesHaveIdSegment: DUMMY_ROUTE_WITH_ID,
segment: $segment,
routeName: $routeName,
controller: $controller
Expand All @@ -33,7 +33,7 @@ trait DummyClassOperation
{
$this->formDefaults(
operationName: 'dummyClass',
// buttonStack: 'line', // alternatives: top, bottom
buttonStack: 'DUMMY_BUTTON_STACK', // alternatives: top, bottom
// buttonMeta: [
// 'icon' => 'la la-home',
// 'label' => 'Dummy Class',
Expand All @@ -48,23 +48,23 @@ trait DummyClassOperation
* Method to handle the GET request and display the View with a Backpack form
*
*/
public function getDummyClassForm(?int $id = null) : \Illuminate\Contracts\View\View
public function getDummyClassForm(DUMMY_FUNCTION_PARAMETERS) : \Illuminate\Contracts\View\View
{
$this->crud->hasAccessOrFail('dummyClass');

return $this->formView($id);
return $this->formView(DUMMY_GETFORM_VIEW_PARAMETER);
}

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

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

Expand Down

0 comments on commit 996a09a

Please sign in to comment.