Skip to content

Commit e7af731

Browse files
authored
Merge pull request #358 from opcodesio/api-only-setting
ability to not register web routes for Log Viewer
2 parents f7ae8b1 + 20a3bab commit e7af731

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

config/log-viewer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
'enabled' => env('LOG_VIEWER_ENABLED', true),
1414

15+
'api_only' => env('LOG_VIEWER_API_ONLY', false),
16+
1517
'require_auth_in_production' => true,
1618

1719
/*

src/LogViewerServiceProvider.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ protected function registerRoutes()
9595
$this->loadRoutesFrom(self::basePath('/routes/api.php'));
9696
});
9797

98-
Route::group([
99-
'domain' => config('log-viewer.route_domain', null),
100-
'prefix' => config('log-viewer.route_path'),
101-
'namespace' => 'Opcodes\LogViewer\Http\Controllers',
102-
'middleware' => config('log-viewer.middleware', null),
103-
], function () {
104-
$this->loadRoutesFrom(self::basePath('/routes/web.php'));
105-
});
98+
if (! config('log-viewer.api_only', false)) {
99+
Route::group([
100+
'domain' => config('log-viewer.route_domain', null),
101+
'prefix' => config('log-viewer.route_path'),
102+
'namespace' => 'Opcodes\LogViewer\Http\Controllers',
103+
'middleware' => config('log-viewer.middleware', null),
104+
], function () {
105+
$this->loadRoutesFrom(self::basePath('/routes/web.php'));
106+
});
107+
}
106108
}
107109

108110
protected function registerResources()

tests/Feature/RoutesTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
4+
35
test('the default url can be changed', function () {
46
config()->set('log-viewer.route_path', 'new-log-route');
57

@@ -25,6 +27,22 @@
2527
expect(route('log-viewer.index'))->toBe('http://localhost');
2628
});
2729

30+
test('only use api', function () {
31+
config()->set('log-viewer.api_only', true);
32+
33+
reloadRoutes();
34+
35+
route('log-viewer.index');
36+
})->throws(RouteNotFoundException::class);
37+
38+
test('only both api and web', function () {
39+
config()->set('log-viewer.api_only', false);
40+
41+
reloadRoutes();
42+
43+
expect(route('log-viewer.index'))->toBe('http://localhost/log-viewer');
44+
});
45+
2846
/*
2947
|--------------------------------------------------------------------------
3048
| HELPERS
@@ -33,5 +51,9 @@
3351

3452
function reloadRoutes(): void
3553
{
54+
// unset any routes that were set previously
55+
app('router')->setRoutes(new \Illuminate\Routing\RouteCollection());
56+
57+
// boot the service provider to register the routes again
3658
(new \Opcodes\LogViewer\LogViewerServiceProvider(app()))->boot();
3759
}

0 commit comments

Comments
 (0)