Skip to content

Instantly share code, notes, and snippets.

@adamwathan
Last active June 11, 2022 19:55
Show Gist options
  • Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.
Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.

Revisions

  1. adamwathan revised this gist Feb 17, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions 1-add-macros.php
    Original 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->route('_format'), function () {
    abort(404);
    }));
    if ($this->route('_format') === null) {
    return value(array_get($responses, $this->format($defaultFormat)));
    }

    return value(array_get($responses, $this->format($defaultFormat)));
    return value(array_get($responses, $this->route('_format'), function () {
    abort(404);
    }));
    });
    }

  2. adamwathan revised this gist Feb 17, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions 1-add-macros.php
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,7 @@ class AppServiceProvider extends ServiceProvider
    public function boot()
    {
    Route::macro('multiformat', function () {
    $this->uri = $this->uri . '.{_format?}';
    return $this;
    return $this->setUri($this->uri() . '.{_format?}');
    });

    Request::macro('match', function ($responses, $defaultFormat = 'html') {
  3. adamwathan revised this gist Feb 17, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 1-add-macros.php
    Original 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;
    });

  4. adamwathan revised this gist Feb 17, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions 1-add-macros.php
    Original 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 . '.{_extension?}';
    $this->uri = $this->uri . '.{_format?}';

    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 () {
    if ($this->route('_format') !== null) {
    return value(array_get($responses, $this->route('_format'), function () {
    abort(404);
    }));
    }
  5. adamwathan revised this gist Feb 17, 2018. 3 changed files with 3 additions and 32 deletions.
    19 changes: 3 additions & 16 deletions 2-add-macros.php → 1-add-macros.php
    Original file line number Diff line number Diff line change
    @@ -17,27 +17,14 @@ 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, '[^\/.]+');
    }
    }

    $this->uri = $this->uri . '{_extension?}';
    $this->where('_extension', '(\..+)');
    $this->middleware(CaptureRequestExtension::class);

    $this->parameterNames = $this->compileParameterNames();
    $this->uri = $this->uri . '.{_extension?}';

    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 () {
    if ($this->route()->parameter('_extension') !== null) {
    return value(array_get($responses, $this->route()->parameter('_extension'), function () {
    abort(404);
    }));
    }
    16 changes: 0 additions & 16 deletions 1-add-middleware.php
    Original file line number Diff line number Diff line change
    @@ -1,16 +0,0 @@
    <?php

    namespace App\Http\Middleware;

    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);
    }
    }
    File renamed without changes.
  6. adamwathan revised this gist Feb 15, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 2-add-macros.php
    Original 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, '[^\/.]+');
    }
  7. adamwathan revised this gist Feb 15, 2018. 6 changed files with 95 additions and 54 deletions.
    4 changes: 0 additions & 4 deletions CaptureRequestExtension.php → 1-add-middleware.php
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,6 @@

    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)
    56 changes: 56 additions & 0 deletions 2-add-macros.php
    Original 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()
    {
    //
    }
    }
    39 changes: 39 additions & 0 deletions 3-use-it.php
    Original 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']);
    }
    ]);
    }
    }
    11 changes: 0 additions & 11 deletions request-macro.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    <?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)));
    });
    18 changes: 0 additions & 18 deletions route-macro.php
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    <?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;
    });
    21 changes: 0 additions & 21 deletions usage.php
    Original file line number Diff line number Diff line change
    @@ -1,21 +0,0 @@
    <?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,
    ]);
    }
  8. adamwathan created this gist Feb 14, 2018.
    20 changes: 20 additions & 0 deletions CaptureRequestExtension.php
    Original 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);
    }
    }
    11 changes: 11 additions & 0 deletions request-macro.php
    Original 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)));
    });
    18 changes: 18 additions & 0 deletions route-macro.php
    Original 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;
    });
    21 changes: 21 additions & 0 deletions usage.php
    Original 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,
    ]);
    }