Skip to content

Commit 967d6b9

Browse files
authored
Merge pull request #166 from Laravel-Backpack/fixes-page-command
Added route and layout options to PageBackpackCommand
2 parents d810369 + 8ea8abd commit 967d6b9

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/Console/Commands/PageBackpackCommand.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class PageBackpackCommand extends GeneratorCommand
2323
*/
2424
protected $signature = 'backpack:page {name}
2525
{--view-path=admin : Path for the view, after resources/views/}
26-
{--layout= : Base layout for the page}';
26+
{--route= : Path for the route, after admin/}
27+
{--layout=blank : Base layout for the page}';
2728

2829
/**
2930
* The console command description.
@@ -53,16 +54,20 @@ public function handle()
5354
->replace('.', '/')
5455
->start('/')
5556
->prepend($this->option('view-path'))
57+
->replace('\\', '/')
5658
->replace('//', '/')
5759
->trim('/');
5860

59-
$name = $input->afterLast('/')->replace('-', '_')->snake();
60-
$path = $input->beforeLast('/');
61-
$filePath = "$path/$name";
62-
$fullpath = $this->getPath($filePath);
63-
$layout = $this->option('layout');
61+
$name = $input->afterLast('/');
62+
$nameSnake = $name->snake();
63+
$nameTitle = $nameSnake->replace('_', ' ')->replace('-', ' ')->title();
64+
$path = $input->beforeLast($name)->trim('/\\');
65+
$filePath = Str::of("$path/$name")->trim('/\\');
66+
$fullPath = $this->getPath($filePath);
67+
$layout = Str::of($this->option('layout'))->replace('\\', '/')->replace('/', '.');
68+
$route = Str::of($this->option('route') ?? $nameSnake)->replace('\\', '/')->trim('/');
6469

65-
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} page");
70+
$this->infoBlock("Creating {$nameTitle} page");
6671

6772
$this->progressBlock("Creating view <fg=blue>resources/views/${filePath}.blade.php</>");
6873

@@ -73,40 +78,37 @@ public function handle()
7378
return false;
7479
}
7580

76-
$this->makeDirectory($fullpath);
81+
$this->makeDirectory($fullPath);
7782

7883
// create page view
7984
$stub = $this->buildClass($filePath);
8085
$stub = str_replace('layout', $layout, $stub);
81-
$stub = str_replace('Dummy Name', $name->replace('_', ' ')->title(), $stub);
82-
$this->files->put($fullpath, $stub);
86+
$stub = str_replace('Dummy Name', $nameTitle, $stub);
87+
$this->files->put($fullPath, $stub);
8388

8489
$this->closeProgressBlock();
8590

86-
// Clean up name
87-
$name = $name->replace('_', ' ')->replace('-', ' ')->title();
88-
8991
// create controller
9092
$this->call('backpack:page-controller', [
91-
'name' => $name,
93+
'name' => $nameTitle,
9294
'--view-path' => $path,
9395
]);
9496

9597
// create route
9698
$this->call('backpack:add-custom-route', [
97-
'code' => "Route::get('{$name->kebab()}', '{$name->studly()}Controller@index')->name('page.{$name->kebab()}.index');",
99+
'code' => "Route::get('{$route}', '{$nameTitle->studly()}Controller@index')->name('page.{$nameSnake}.index');",
98100
]);
99101

100102
// create the sidebar item
101103
$this->call('backpack:add-sidebar-content', [
102-
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('{$name->kebab()}') }}\"><i class=\"nav-icon la la-question\"></i> {$name}</a></li>",
104+
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('{$route}') }}\"><i class=\"nav-icon la la-question\"></i> {$nameTitle}</a></li>",
103105
]);
104106

105-
$url = Str::of(config('app.url'))->finish('/')->append("admin/{$name->kebab()}");
107+
$url = backpack_url($route);
106108

107109
$this->newLine();
108-
$this->note("Page {$name} created.");
109-
$this->note("Go to <fg=blue>$url</> to access your new page.");
110+
$this->note("Page {$nameTitle} created.");
111+
$this->note("Go to <fg=blue>{$url}</> to access your new page.");
110112
$this->newLine();
111113
}
112114

src/Console/Commands/PageControllerBackpackCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class PageControllerBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:page-controller {name} {--view-path=}';
25+
protected $signature = 'backpack:page-controller {name}
26+
{--view-path=admin : Path of the view, after resources/views/}';
2627

2728
/**
2829
* The console command description.
@@ -158,8 +159,8 @@ protected function replaceNameStrings(&$stub)
158159
{
159160
$name = $this->getNameInput();
160161

161-
$stub = str_replace('DummyName', $name, $stub);
162-
$stub = str_replace('dummyName', lcfirst($name), $stub);
162+
$stub = str_replace('DummyName', $name->value, $stub);
163+
$stub = str_replace('dummyName', lcfirst($name->value), $stub);
163164
$stub = str_replace('Dummy Name', $name->kebab()->replace('-', ' ')->title(), $stub);
164165

165166
return $this;

src/Console/stubs/page.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
@extends(backpack_view('blank'))
1+
@extends(backpack_view('layout'))
22

33
@section('content')
44
<div class="jumbotron">
55
<h1 class="mb-4">Dummy Name</h1>
66

77
<p>Go to <code>{{ $page }}</code> to edit this view or <code>{{ $controller }}</code> to edit the controller.</p>
88
</div>
9-
@endsection
9+
@endsection

0 commit comments

Comments
 (0)