From 12d283d2a2efdd5baac697804d0bc82379faec01 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 26 Jun 2020 13:43:55 -0400 Subject: [PATCH 1/2] Remove Router macro --- README.md | 25 +----- src/Macro.php | 53 ------------ src/ZiggyServiceProvider.php | 13 +-- tests/Unit/MacroTest.php | 154 ----------------------------------- 4 files changed, 3 insertions(+), 242 deletions(-) delete mode 100644 src/Macro.php delete mode 100644 tests/Unit/MacroTest.php diff --git a/README.md b/README.md index 115645fd..80d4021a 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,7 @@ Ziggy supports all versions of Laravel from `5.4` to `7.x`. - [Default Values](#default-values) - [Filtering Routes](#filtering-routes) - [Basic Filtering](#basic-filtering) - - [Basic Filtering using Macros](#basic-filtering-using-macros) - - [Advanced Filtering using Groups](#advanced-filtering-using-groups) + - [Filtering using Groups](#filtering-using-groups) - [Other Useful Methods](#other-useful-methods) - [`current()`](#current) - [`check()`](#check) @@ -140,27 +139,7 @@ return [ As shown in the example above, Ziggy can use asterisks as wildcards in route filter patterns. `home` will only match the route named `home`, whereas `api.*` will match any route whose name begins with `api.`, such as `api.posts.index` and `api.users.show`. -#### Basic Filtering using Macros - -You can also filter routes using the following macros: - -```php -Route::only(function () { - Route::get('...')->name('posts'); -}); - -Route::only()->get('...')->name('posts'); -``` - -```php -Route::except(function () { - Route::get('...')->name('posts'); -}); - -Route::except()->get('...')->name('posts'); -``` - -#### Advanced Filtering using Groups +#### Filtering using Groups You can also optionally define multiple groups of included routes using a `groups` key in your `config/ziggy.php`: diff --git a/src/Macro.php b/src/Macro.php deleted file mode 100644 index 65586279..00000000 --- a/src/Macro.php +++ /dev/null @@ -1,53 +0,0 @@ -list = $list; - $this->router = $router; - $this->group = $group; - } - - public function compile() - { - if (is_callable($this->group)) { - $this->router->group(['listed_as' => $this->list], $this->group); - } - - return $this; - } - - public static function only(Router $router, $group = null) - { - return (new static($router, static::ONLY, $group))->compile(); - } - - public static function except(Router $router, $group = null) - { - return (new static($router, static::EXCEPT, $group))->compile(); - } - - public function __call($method, $parameters) - { - $route = call_user_func_array([$this->router, $method], $parameters); - - switch ($this->list) { - case static::EXCEPT: - $route->listedAs = 'except'; - break; - case static::ONLY: - $route->listedAs = 'only'; - break; - } - - return $route; - } -} diff --git a/src/ZiggyServiceProvider.php b/src/ZiggyServiceProvider.php index e0ccbed0..46e8c77c 100644 --- a/src/ZiggyServiceProvider.php +++ b/src/ZiggyServiceProvider.php @@ -2,7 +2,6 @@ namespace Tightenco\Ziggy; -use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Illuminate\View\Compilers\BladeCompiler; @@ -10,14 +9,6 @@ class ZiggyServiceProvider extends ServiceProvider { public function boot() { - Route::macro('except', function ($group = null) { - return Macro::except($this, $group); - }); - - Route::macro('only', function ($group = null) { - return Macro::only($this, $group); - }); - if ($this->app->resolved('blade.compiler')) { $this->registerDirective($this->app['blade.compiler']); } else { @@ -27,9 +18,7 @@ public function boot() } if ($this->app->runningInConsole()) { - $this->commands([ - CommandRouteGenerator::class, - ]); + $this->commands(CommandRouteGenerator::class); } } diff --git a/tests/Unit/MacroTest.php b/tests/Unit/MacroTest.php deleted file mode 100644 index 6a50ead9..00000000 --- a/tests/Unit/MacroTest.php +++ /dev/null @@ -1,154 +0,0 @@ -except(function ($router) { - $router->get('/posts', function () { return ''; }); - $router->get('/posts/show', function () { return ''; }) - ->name('posts.show'); - }); - - $router->get('/tags/{tag}', function () { return ''; }) - ->name('tags.show'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['tags.show'], array_keys($payload->toArray())); - } - - /** @test */ - function only_matching_routes_excluded_with_except_fluent_method_are_filtered() - { - $router = app('router'); - - $router->get('/', function () { return ''; }); - - $router->except() - ->get('/pages', function () { return ''; }) - ->name('pages.index'); - - $router->get('/pages/{slug}', function () { return ''; }) - ->name('pages.show'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['pages.show'], array_keys($payload->toArray())); - } - - /** @test */ - function only_matching_routes_excluded_with_except_fluent_method_and_group_are_filtered() - { - $router = app('router'); - - $router->get('/', function () { return ''; }); - - $router->except(function ($router) { - $router->get('/posts', function () { return ''; }); - $router->get('/posts/show', function () { return ''; }) - ->name('posts.show'); - }); - - $router->get('/tags/{tag}', function () { return ''; }) - ->name('tags.show'); - - $router->except() - ->get('/pages', function () { return ''; }) - ->name('pages.index'); - - $router->get('/pages/{slug}', function () { return ''; }) - ->name('pages.show'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['tags.show', 'pages.show'], array_keys($payload->toArray())); - } - - /** @test */ - function only_matching_routes_included_with_only_fluent_method_are_filtered() - { - $router = app('router'); - - $router->get('/tags/{tag}', function () { return ''; }) - ->name('tags.show'); - - $router->only() - ->get('/pages', function () { return ''; }) - ->name('pages.index'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['pages.index'], array_keys($payload->toArray())); - } - - /** @test */ - function only_matching_routes_included_with_only_group_method_are_filtered() - { - $router = app('router'); - - $router->get('/', function () { return ''; }); - - $router->only(function ($router) { - $router->get('/posts', function () { return ''; }); - $router->get('/posts/show', function () { return ''; }) - ->name('posts.show'); - }); - - $router->get('/tags/{tag}', function () { return ''; }) - ->name('tags.show'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['posts.show'], array_keys($payload->toArray())); - } - - /** @test */ - function only_matching_routes_included_with_only_group_and_fluent_method_are_filtered() - { - $router = app('router'); - - $router->get('/', function () { return ''; }); - - $router->only(function ($router) { - $router->get('/posts', function () { return ''; }); - $router->get('/posts/show', function () { return ''; }) - ->name('posts.show'); - }); - - $router->get('/tags/{tag}', function () { return ''; }) - ->name('tags.show'); - - $router->only() - ->get('/pages', function () { return ''; }) - ->name('pages.index'); - - $router->get('/pages/{slug}', function () { return ''; }) - ->name('pages.show'); - - $router->getRoutes()->refreshNameLookups(); - - $payload = RoutePayload::compile($router); - - $this->assertEquals(['posts.show', 'pages.index'], array_keys($payload->toArray())); - } -} From 26c694ea05d9a1c2cdac8c9118d95f6b6d819c57 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 26 Jun 2020 13:47:30 -0400 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 456cfae5..61500764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ Breaking changes are marked with ⚠️. - ⚠️ Update `ziggy:generate` output path for Laravel 5.7+ `resources` directory structure, thanks [@Somethingideally](https://github.com/Somethingideally)! ([#269](https://github.com/tightenco/ziggy/pull/269)) - ⚠️ Update automatic `id` parameter detection to check for higher priority named route parameters and allow passing `id` as a query parameter ([#301](https://github.com/tightenco/ziggy/pull/301)) +**Removed** + +- ⚠️ Remove `Route` Facade macros `Route::only()` and `Route::except()` (previously `Route::whitelist()` and `Route::blacklist()`) ([#306](https://github.com/tightenco/ziggy/pull/306)) + **Fixed** - Fix automatic `id` parameter detection by also excluding routes with an _optional_ `id` parameter (`{id?}`), thanks [@Livijn](https://github.com/Livijn)! ([#263](https://github.com/tightenco/ziggy/pull/263))