Skip to content

Commit ee0c18c

Browse files
Merge pull request #805 from joshmanders/feature/support-passing-except-only-args-to-ziggy-command
feat: support passing except/only args to ziggy command
2 parents 6612c8c + 33a4b16 commit ee0c18c

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ Route::get('ziggy', fn () => response()->json(new Ziggy));
550550

551551
### Re-generating the routes file when your app routes change
552552

553-
If you are generating your Ziggy config as a file by running `php artisan ziggy:generate`, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to [Nuno Rodrigues](https://github.com/nacr) for [the idea and a sample implementation](https://github.com/tighten/ziggy/issues/321#issuecomment-689150082). See [#655 for a Vite example](https://github.com/tighten/ziggy/pull/655/files#diff-4aeb78f813e14842fcf95bdace9ced23b8a6eed60b23c165eaa52e8db2f97b61).
553+
If you are generating your Ziggy config as a file by running `php artisan ziggy:generate`, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to [Nuno Rodrigues](https://github.com/nacr) for [the idea and a sample implementation](https://github.com/tighten/ziggy/issues/321#issuecomment-689150082). See [#655](https://github.com/tighten/ziggy/pull/655/files#diff-4aeb78f813e14842fcf95bdace9ced23b8a6eed60b23c165eaa52e8db2f97b61) or [vite-plugin-ziggy](https://github.com/aniftyco/vite-plugin-ziggy) for Vite examples.
554554

555555
<details>
556556
<summary>Laravel Mix plugin example</summary>

src/CommandRouteGenerator.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ class CommandRouteGenerator extends Command
1515
{--types : Generate a TypeScript declaration file.}
1616
{--types-only : Generate only a TypeScript declaration file.}
1717
{--url=}
18-
{--group=}';
18+
{--group=}
19+
{--except= : Route name patterns to exclude.}
20+
{--only= : Route name patterns to include.}';
1921

2022
protected $description = 'Generate a JavaScript file containing Ziggy’s routes and configuration.';
2123

2224
public function handle(Filesystem $filesystem)
2325
{
2426
$ziggy = new Ziggy($this->option('group'), $this->option('url') ? url($this->option('url')) : null);
2527

28+
if ($this->option('except') && ! $this->option('only')) {
29+
$ziggy->filter(explode(',', $this->option('except')), false);
30+
} else if ($this->option('only') && ! $this->option('except')) {
31+
$ziggy->filter(explode(',', $this->option('only')));
32+
}
33+
2634
$path = $this->argument('path') ?? config('ziggy.output.path', 'resources/js/ziggy.js');
2735

2836
if ($filesystem->isDirectory(base_path($path))) {

tests/Unit/CommandRouteGeneratorTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@
6666
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
6767
});
6868

69+
test('generate file using --except option', function () {
70+
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
71+
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
72+
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by options
73+
74+
artisan('ziggy:generate --except=admin.*');
75+
76+
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
77+
});
78+
79+
test('generate file using --only option', function () {
80+
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
81+
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
82+
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by options
83+
84+
artisan('ziggy:generate --only=postComments.index,slashes');
85+
86+
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
87+
});
88+
89+
test('generate file using both --only and --except', function () {
90+
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
91+
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
92+
93+
artisan('ziggy:generate --only=slashes --except=postComments.index');
94+
95+
// Options cancel each other out and are ignored
96+
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
97+
});
98+
6999
test('generate file with custom output formatter', function () {
70100
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
71101
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config
@@ -162,7 +192,7 @@
162192

163193
class CustomFile extends File
164194
{
165-
function __toString(): string
195+
public function __toString(): string
166196
{
167197
return <<<JAVASCRIPT
168198
// This is a custom template

0 commit comments

Comments
 (0)