From 3dd515a4e992342f0779fb865ddbc0eed87a0b30 Mon Sep 17 00:00:00 2001 From: austenc Date: Fri, 16 Feb 2018 23:49:16 -0700 Subject: [PATCH 1/2] added route multiformat and route match macros --- src/Preset.php | 6 +++ .../app/Providers/AppServiceProvider.php | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/stubs/app/Providers/AppServiceProvider.php diff --git a/src/Preset.php b/src/Preset.php index 41cb375..1811ef8 100644 --- a/src/Preset.php +++ b/src/Preset.php @@ -19,6 +19,7 @@ public static function install() static::updateTemplates(); static::removeNodeModules(); static::updateGitignore(); + static::addMacros(); } protected static function updatePackageArray(array $packages) @@ -75,4 +76,9 @@ protected static function updateGitignore() { copy(__DIR__.'/stubs/gitignore-stub', base_path('.gitignore')); } + + protected static function addMacros() + { + copy(__DIR__.'/stubs/app/Providers/AppServiceProvider.php', base_path('app/Providers/AppServiceProvider.php')); + } } diff --git a/src/stubs/app/Providers/AppServiceProvider.php b/src/stubs/app/Providers/AppServiceProvider.php new file mode 100644 index 0000000..4a2d65e --- /dev/null +++ b/src/stubs/app/Providers/AppServiceProvider.php @@ -0,0 +1,38 @@ +uri = $this->uri . '.{_extension?}'; + return $this; + }); + Request::macro('match', function ($responses, $defaultFormat = 'html') { + if ($this->route()->parameter('_extension') !== null) { + return value(array_get($responses, $this->route()->parameter('_extension'), function () { + abort(404); + })); + } + return value(array_get($responses, $this->format($defaultFormat))); + }); + } + /** + * Register any application services. + * + * @return void + */ + public function register() + { + // + } +} \ No newline at end of file From b5ce7be1e2c9d6aa4b6015ba7dac6401c4f47412 Mon Sep 17 00:00:00 2001 From: austenc Date: Sat, 17 Feb 2018 09:24:42 -0700 Subject: [PATCH 2/2] updated macro methods to even simpler version --- src/stubs/app/Providers/AppServiceProvider.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/stubs/app/Providers/AppServiceProvider.php b/src/stubs/app/Providers/AppServiceProvider.php index 4a2d65e..e01247a 100644 --- a/src/stubs/app/Providers/AppServiceProvider.php +++ b/src/stubs/app/Providers/AppServiceProvider.php @@ -14,16 +14,13 @@ class AppServiceProvider extends ServiceProvider public function boot() { Route::macro('multiformat', function () { - $this->uri = $this->uri . '.{_extension?}'; - return $this; + return $this->setUri($this->uri() . '.{_format?}'); }); + Request::macro('match', function ($responses, $defaultFormat = 'html') { - if ($this->route()->parameter('_extension') !== null) { - return value(array_get($responses, $this->route()->parameter('_extension'), function () { - abort(404); - })); - } - return value(array_get($responses, $this->format($defaultFormat))); + return value(array_get($responses, $this->route()->parameter('_format', $this->format($defaultFormat)), function () { + abort(404); + })); }); } /**