Last active
June 11, 2022 19:55
-
-
Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.
Revisions
-
adamwathan revised this gist
Feb 17, 2018 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,13 +21,13 @@ public function boot() }); Request::macro('match', function ($responses, $defaultFormat = 'html') { if ($this->route('_format') === null) { return value(array_get($responses, $this->format($defaultFormat))); } return value(array_get($responses, $this->route('_format'), function () { abort(404); })); }); } -
adamwathan revised this gist
Feb 17, 2018 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,7 @@ class AppServiceProvider extends ServiceProvider public function boot() { Route::macro('multiformat', function () { return $this->setUri($this->uri() . '.{_format?}'); }); Request::macro('match', function ($responses, $defaultFormat = 'html') { -
adamwathan revised this gist
Feb 17, 2018 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,6 @@ public function boot() { Route::macro('multiformat', function () { $this->uri = $this->uri . '.{_format?}'; return $this; }); -
adamwathan revised this gist
Feb 17, 2018 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,14 +17,14 @@ class AppServiceProvider extends ServiceProvider public function boot() { Route::macro('multiformat', function () { $this->uri = $this->uri . '.{_format?}'; return $this; }); Request::macro('match', function ($responses, $defaultFormat = 'html') { if ($this->route('_format') !== null) { return value(array_get($responses, $this->route('_format'), function () { abort(404); })); } -
adamwathan revised this gist
Feb 17, 2018 . 3 changed files with 3 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,27 +17,14 @@ class AppServiceProvider extends ServiceProvider public function boot() { Route::macro('multiformat', function () { $this->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); })); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +0,0 @@ File renamed without changes. -
adamwathan revised this gist
Feb 15, 2018 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,10 @@ class AppServiceProvider extends ServiceProvider public function boot() { Route::macro('multiformat', function () { // Hello darkness, my old friend if (count($this->parameterNames()) > 0 && ends_with($this->uri(), '}')) { $lastParameter = array_last($this->parameterNames()); // I've come to talk with you again if (! isset($this->wheres[$lastParameter])) { $this->where($lastParameter, '[^\/.]+'); } -
adamwathan revised this gist
Feb 15, 2018 . 6 changed files with 95 additions and 54 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,6 @@ namespace App\Http\Middleware; class CaptureRequestExtension { public function handle($request, $next) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ <?php namespace App\Providers; use Illuminate\Http\Request; use Illuminate\Routing\Route; use Illuminate\Support\ServiceProvider; use App\Http\Middleware\CaptureRequestExtension; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Route::macro('multiformat', function () { if (count($this->parameterNames()) > 0 && ends_with($this->uri(), '}')) { $lastParameter = array_last($this->parameterNames()); if (! isset($this->wheres[$lastParameter])) { $this->where($lastParameter, '[^\/.]+'); } } $this->uri = $this->uri . '{_extension?}'; $this->where('_extension', '(\..+)'); $this->middleware(CaptureRequestExtension::class); $this->parameterNames = $this->compileParameterNames(); return $this; }); Request::macro('match', function ($responses, $defaultFormat = 'html') { if ($this->attributes->get('_extension') !== null) { return value(array_get($responses, $this->attributes->get('_extension'), function () { abort(404); })); } return value(array_get($responses, $this->format($defaultFormat))); }); } /** * Register any application services. * * @return void */ public function register() { // } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ <?php /** * Mark a route as 'multiformat' to allow different extensions (html, json, xml, etc.) * * This route will match all of these requests: * /podcasts/4 * /podcasts/4.json * /podcasts/4.html * /podcasts/4.zip */ Route::get('/podcasts/{id}', 'PodcastsController@show')->multiformat(); /** * Use `Request::match()` to return the right response for the requested format. * * Supports closures to avoid doing unnecessary work, and returns 404 if the * requested format is not supported. * * Will also take into account the `Accept` header if no extension is provided. */ class PodcastsController { public function show($id) { $podcast = Podcast::findOrFail($id); return request()->match([ 'html' => view('podcasts.show', [ 'podcast' => $podcast, 'episodes' => $podcast->recentEpisodes(5), ]), 'json' => $podcast, 'xml' => function () use ($podcast) { return response($podcast->toXml(), 200, ['Content-Type' => 'text/xml']); } ]); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +0,0 @@ -
adamwathan created this gist
Feb 14, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ <?php namespace App\Http\Middleware; use Illuminate\Support\Facades\Route; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; class CaptureRequestExtension { public function handle($request, $next) { if ($request->route()->parameter('_extension') !== null) { $request->attributes->set('_extension', substr($request->route()->parameter('_extension'), 1)); $request->route()->forgetParameter('_extension'); } return $next($request); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ <?php Request::macro('match', function ($responses, $defaultFormat = 'html') { if ($this->attributes->get('_extension') !== null) { return value(array_get($responses, $this->attributes->get('_extension'), function () { abort(404); })); } return value(array_get($responses, $this->format($defaultFormat))); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ <?php Route::macro('multitype', function () { if (count($this->parameterNames()) > 0 && ends_with($this->uri(), '}')) { $lastParameter = array_last($this->parameterNames()); if (! isset($this->wheres[$lastParameter])) { $this->where($lastParameter, '[^.]+'); } } $this->uri = $this->uri . '{_extension?}'; $this->where('_extension', '(\..+)'); $this->middleware(CaptureRequestExtension::class); $this->parameterNames = $this->compileParameterNames(); return $this; }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?php // In routes file: Route::get('/podcasts/{id}', 'PodcastsController@show')->multitype(); // In controller: public function show($id) { $podcast = Podcast::findOrFail($id); abort_unless($podcast->isVisibleTo(Auth::user()), 404); return request()->match([ 'html' => view('podcasts.show', [ 'podcast' => $podcast, 'episodes' => $podcast->recentEpisodes(5), ]), 'json' => $podcast, ]); }