php Laravel中的多格式端点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel中的多格式端点相关的知识,希望对你有一定的参考价值。

<?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']);
            }
        ]);
    }
}
<?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 () {
            // 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();

            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()
    {
        //
    }
}
<?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);
    }
}

以上是关于php Laravel中的多格式端点的主要内容,如果未能解决你的问题,请参考以下文章

php Laravel中的多对多关系来自一系列ID Raw

从 Laravel 中的多对多关系获取列值

从laravel中的多对多关系中获取单列

php Laravel:HasDatatable Trait API端点

php Laravel的多角色权限中间件

带有 laravel 的 Paypal PHP SDK。在生产模式下加载配置端点时出现问题