Skip to content

Commit 003d015

Browse files
authored
Merge pull request #4590 from Laravel-Backpack/prettier-backpack-crud-command
Improved UI for both custom route command and add sidebar content command
2 parents cfb8c07 + 63eba2b commit 003d015

File tree

3 files changed

+78
-52
lines changed

3 files changed

+78
-52
lines changed

src/app/Console/Commands/AddCustomRouteContent.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class AddCustomRouteContent extends Command
1010
{
11+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
1113
/**
1214
* The name and signature of the console command.
1315
*
@@ -45,32 +47,40 @@ public function handle()
4547
$disk = Storage::disk($disk_name);
4648
$code = $this->argument('code');
4749

48-
if ($disk->exists($path)) {
49-
$old_file_path = $disk->path($path);
50+
$this->progressBlock("Adding route to $path");
5051

51-
// insert the given code before the file's last line
52-
$file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES);
52+
// Validate file exists
53+
if (! $disk->exists($path)) {
54+
Artisan::call('vendor:publish', ['--provider' => \Backpack\CRUD\BackpackServiceProvider::class, '--tag' => 'custom_routes']);
55+
$this->handle();
5356

54-
// if the code already exists in the file, abort
55-
if ($this->getLastLineNumberThatContains($code, $file_lines)) {
56-
return $this->comment('Route already existed.');
57-
}
57+
return;
58+
}
5859

59-
$end_line_number = $this->customRoutesFileEndLine($file_lines);
60-
$file_lines[$end_line_number + 1] = $file_lines[$end_line_number];
61-
$file_lines[$end_line_number] = ' '.$code;
62-
$new_file_content = implode(PHP_EOL, $file_lines);
60+
// insert the given code before the file's last line
61+
$old_file_path = $disk->path($path);
62+
$file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES);
6363

64-
if ($disk->put($path, $new_file_content)) {
65-
$this->info('Successfully added code to '.$path);
66-
} else {
67-
$this->error('Could not write to file: '.$path);
68-
}
69-
} else {
70-
Artisan::call('vendor:publish', ['--provider' => 'Backpack\CRUD\BackpackServiceProvider', '--tag' => 'custom_routes']);
64+
// if the code already exists in the file, abort
65+
if ($this->getLastLineNumberThatContains($code, $file_lines)) {
66+
$this->closeProgressBlock('Already existed', 'yellow');
7167

72-
$this->handle();
68+
return;
7369
}
70+
71+
$end_line_number = $this->customRoutesFileEndLine($file_lines);
72+
$file_lines[$end_line_number + 1] = $file_lines[$end_line_number];
73+
$file_lines[$end_line_number] = ' '.$code;
74+
$new_file_content = implode(PHP_EOL, $file_lines);
75+
76+
if (! $disk->put($path, $new_file_content)) {
77+
$this->errorProgressBlock();
78+
$this->note('Could not write to file.', 'red');
79+
80+
return;
81+
}
82+
83+
$this->closeProgressBlock();
7484
}
7585

7686
private function customRoutesFileEndLine($file_lines)

src/app/Console/Commands/AddSidebarContent.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class AddSidebarContent extends Command
99
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
1012
/**
1113
* The name and signature of the console command.
1214
*
@@ -44,22 +46,34 @@ public function handle()
4446
$disk = Storage::disk($disk_name);
4547
$code = $this->argument('code');
4648

47-
if ($disk->exists($path)) {
48-
$contents = $disk->get($path);
49-
$file_lines = file($disk->path($path), FILE_IGNORE_NEW_LINES);
50-
51-
if ($this->getLastLineNumberThatContains($code, $file_lines)) {
52-
return $this->comment('Sidebar item already existed.');
53-
}
54-
55-
if ($disk->put($path, $contents.PHP_EOL.$code)) {
56-
$this->info('Successfully added code to sidebar_content file.');
57-
} else {
58-
$this->error('Could not write to sidebar_content file.');
59-
}
60-
} else {
61-
$this->error('The sidebar_content file does not exist. Make sure Backpack is properly installed.');
49+
$this->progressBlock("Adding sidebar entry to $path");
50+
51+
// Validate file exists
52+
if (! $disk->exists($path)) {
53+
$this->errorProgressBlock();
54+
$this->note('The sidebar_content file does not exist. Make sure Backpack is properly installed.', 'red');
55+
56+
return;
6257
}
58+
59+
$contents = $disk->get($path);
60+
$file_lines = file($disk->path($path), FILE_IGNORE_NEW_LINES);
61+
62+
// Validate the entry already exists
63+
if ($this->getLastLineNumberThatContains($code, $file_lines)) {
64+
$this->closeProgressBlock('Already existed', 'yellow');
65+
66+
return;
67+
}
68+
69+
if (! $disk->put($path, $contents.PHP_EOL.$code)) {
70+
$this->errorProgressBlock();
71+
$this->note('Could not write to sidebar_content file.', 'red');
72+
73+
return;
74+
}
75+
76+
$this->closeProgressBlock();
6377
}
6478

6579
/**

src/resources/views/base/inc/getting_started.blade.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
<p>
1616
<code class="text-primary bg-light p-1 rounded">php artisan backpack:crud user</code>
1717
</p>
18-
<p>Go ahead, run it in your terminal. You'll notice it has:</p>
19-
<ul>
20-
<li>added an item to the sidebar, in <code class="text-primary bg-light p-1 rounded">resources/views/vendor/backpack/base/inc/sidebar_content.blade.php</code></li>
21-
<li>added a route, inside <code class="text-primary bg-light p-1 rounded">routes/backpack/custom.php</code></li>
22-
<li>created <code class="text-primary bg-light p-1 rounded">app/Http/Controllers/Admin/UserCrudController.php</code></li>
23-
<li>created <code class="text-primary bg-light p-1 rounded">app/Http/Requests/UserRequest.php</code></li>
24-
</ul>
25-
<p>You can now click on the new sidebar item (or <a href="{{ backpack_url('user') }}">here</a>) and you'll be able to see the entries in the <code class="text-primary bg-light p-1 rounded">users</code> table. Even though generated CRUDs work out-of-the-box, they might not be <i>exactly</i> what you need. But that's where Backpack shines, in how easy it is to customize.</p>
18+
<p>Go ahead, run it in your terminal. You can now click on the new sidebar item (or <a href="{{ backpack_url('user') }}">here</a>) and you'll be able to see the entries in the <code class="text-primary bg-light p-1 rounded">users</code> table. Even though generated CRUDs work out-of-the-box, they might not be <i>exactly</i> what you need. But that's where Backpack shines, in how easy it is to customize.</p>
2619

27-
<p>To dig a little deeper, <a href="#" data-toggle="collapse" data-target="#customizeUsersCRUD" aria-expanded="true" aria-controls="customizeUsersCRUD">let's make a few changes to the Users CRUD</a>.</p>
20+
<p>To dig a little deeper, <a href="#" data-toggle="collapse" data-target="#customizeUsersCRUD" aria-expanded="true" aria-controls="customizeUsersCRUD">let's make a few changes to the Users CRUD <i class="la la-angle-double-right"></i></a></p>
2821

2922
<div class="collapse" id="customizeUsersCRUD">
30-
<p><strong>1. Let's remove the "password" column</strong> - no point in showing it. To do that, go to <code class="text-primary bg-light p-1 rounded">UserCrudController::setupListOperation()</code> and remove the line saying <code class="text-primary bg-light p-1 rounded">CRUD::column('password');</code>. Easy-peasy, right?</p>
23+
<p><strong>1. When Listing, let's remove the "password" column</strong> - no point in showing the hash. To do that, go to <code class="text-primary bg-light p-1 rounded">UserCrudController::setupListOperation()</code> and remove the line saying <code class="text-primary bg-light p-1 rounded">CRUD::column('password');</code> - easy-peasy, right?</p>
3124
<p><strong>2. On Create & Update, let's add validation to forms</strong>. There are <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validation" target="_blank">multiple ways to add validation (A, B, C)</a>. Let's change the standard <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validating-fields-using-formrequests" target="_blank">validation using FormRequests</a> (A), to a simpler validation using <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validating-fields-using-field-attributes" target="_blank">field attributes</a> (C):</p>
3225
<ul>
3326
<li>to remove the current validation:<br>
@@ -39,17 +32,17 @@
3932
</li>
4033
<li>a quick way to add validation is to go to <code class="text-primary bg-light p-1 rounded">setupCreateOperation()</code> and specify validation rules directly on the fields:
4134
<p>
42-
<pre class="text-primary bg-light p-1 rounded">
35+
<pre class="language-php rounded"><code class="language-php p-1">
4336
CRUD::field('name')->validationRules('required|min:5');
4437
CRUD::field('email')->validationRules('required|email|unique:users,email');
4538
CRUD::field('password')->validationRules('required');
46-
</pre>
39+
</code></pre>
4740
</p>
4841
</li>
4942
</ul>
5043
<p><strong>3. On Create, let's hash the password.</strong> Currently, if we create a new User, it'll work. But if you look in the database... you'll notice the password is stored in plain text. We don't want that - we want it hashed. There are <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#use-events-in-your-setup-method" target="_blank">multiple ways to achieve this too</a>. Let's use Model Events inside <code class="text-primary bg-light p-1 rounded">setupCreateOperation()</code>. Here's how our method could look, when we also tap into the <code class="text-primary bg-light p-1 rounded">creating</code> event, to hash the password:</p>
5144
<p>
52-
<pre class="text-primary bg-light p-1 rounded">
45+
<pre class="language-php rounded"><code class="language-php p-1">
5346
protected function setupCreateOperation()
5447
{
5548
CRUD::field('name')->validationRules('required|min:5');
@@ -60,11 +53,11 @@ protected function setupCreateOperation()
6053
$entry->password = \Hash::make($entry->password);
6154
});
6255
}
63-
</pre>
56+
</code></pre>
6457
</p>
65-
<p><strong>4. On Update, let's not require the password</strong>. It should only be needed if an admin wants to change it, right? That means the validation rules will be different for "password". But then again... the rules will also be different for "email", right? On Update, we need to pass the ID to the unique rule in Laravel. Since 2/3 rules are different, let's just delete what was inside <code class="text-primary bg-light p-1 rounded">setupUpdateOperation()</code> and code it from scratch:</p>
58+
<p><strong>4. On Update, let's not require the password</strong>. It should only be needed if an admin wants to change it, right? That means the validation rules will be different for "password". But then again... the rules will also be different for "email" (because on Update, we need to pass the ID to the unique rule in Laravel). Since 2/3 rules are different, let's just delete what was inside <code class="text-primary bg-light p-1 rounded">setupUpdateOperation()</code> and code it from scratch:</p>
6659
<p>
67-
<pre class="text-primary bg-light p-1 rounded">
60+
<pre class="language-php rounded"><code class="language-php p-1">
6861
protected function setupUpdateOperation()
6962
{
7063
CRUD::field('name')->validationRules('required|min:5');
@@ -79,7 +72,7 @@ protected function setupUpdateOperation()
7972
}
8073
});
8174
}
82-
</pre>
75+
</code></pre>
8376
</p>
8477
<p>
8578
That's it. You have a working Users CRUD. Plus, you've already learned some advanced techniques, like <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#use-events-in-your-setup-method" target="_blank">using Model events inside CrudController</a> and <a href="https://backpackforlaravel.com/docs/5.x/crud-operation-create#b-validating-fields-using-a-rules-array" target="_blank">using field-attribute-validation instead of form-request-validation</a>. Of course, this only scratches the surface of what Backpack can do. To really understand how it works, and how you can best use Backpack's features, <strong>we heavily recommend you move on to the next step, and learn the basics.</strong>
@@ -116,3 +109,12 @@ protected function setupUpdateOperation()
116109
<p class="mt-3 mb-0"><small>* this card is only visible on <i>localhost</i>. Follow the last step to hide it from <i>localhost</i> too.</small></p>
117110
</div>
118111
</div>
112+
113+
@push('after_styles')
114+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/base16/dracula.min.css">
115+
@endpush
116+
117+
@push('after_scripts')
118+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
119+
<script>hljs.highlightAll();</script>
120+
@endpush

0 commit comments

Comments
 (0)