c# api 定义路由_将自定义路由添加到WordPress REST API

本文详细介绍了如何利用c#在WordPress REST API中定义自定义路由和端点,包括添加路由、设置端点、回调函数、权限回调及处理响应。文章通过示例展示了如何创建包含产品信息的API端点,并强调了REST API的可定制性和灵活性,鼓励开发者创建自己的API以满足特定需求。
摘要由CSDN通过智能技术生成

c# api 定义路由

This article on custom routes was originally published by Torque Magazine, and is reproduced here with permission.

这篇有关自定义路线的文章最初由Torque Magazine发行 ,并在获得许可的情况下在此处转载。

Most of the discussion around the WordPress REST API has been about querying the default routes. In that sense, we’re treating it as a monolithic API — like the Twitter API, for example.

围绕WordPress REST API的大多数讨论都是关于查询默认路由的。 从这个意义上讲,我们将其视为整体API,例如Twitter API。

The truth is, however, that the WordPress REST API is not one API, but millions of highly customizable APIs, which can also be leveraged as a tool for making APIs. Yes, it comes with default routes, but, by necessity, those routes are a compromise between tens of millions of sites, including many that haven’t been made yet.

然而,事实是,WordPress REST API并不是一个API,而是数百万个高度可定制的API,也可以将其用作制作API的工具。 是的,它带有默认路由,但是,根据需要,这些路由是数千万个站点之间的折衷方案,其中包括许多尚未建立的站点。

Just like WordPress isn’t just the global WP_Query object, the REST API isn’t just the default API. Sticking to defaults is like having a traditional WordPress project without ever creating your own WP_Query object, or overwriting the default query at pre_get_posts. It’s possible, but not every job can be done with the default WordPress URL routes alone.

就像WordPress不仅是全局WP_Query对象一样,REST API也不只是默认API。 坚持默认值就像拥有一个传统的WordPress项目,而无需创建自己的WP_Query对象,或覆盖pre_get_posts的默认查询。 有可能,但是并非所有工作都能单独使用默认的WordPress URL路由来完成。

The same is true with the REST API.

REST API也是如此。

In a recent interview with the REST API’s co-lead developer Ryan McCue, he talked about how version two of the project is split into two parts — default routes and the infrastructure for creating RESTful APIs. The default routes provide great examples of how to create your own routes.

在最近一次与REST API共同负责的开发人员Ryan McCue的访谈中,他谈到了如何将项目的第二版分为两部分-默认路由和用于创建RESTful API的基础结构。 默认路由提供了有关如何创建自己的路由的出色示例。

The system used for adding these routes and endpoints is incredibly well done. I’ll be showing you the basics of how to use it in this article; and, as an example, I’ll demonstrate how to create a custom route with two endpoints that show information about products in an eCommerce site powered by Easy Digital Downloads (EDD). This example is based on an API add-on that I built for my own site. If you want to see the full source on GitHub, or the API in action, you can.

用于添加这些路由和端点的系统做得非常好。 在本文中,我将向您展示如何使用它的基础知识。 作为示例,我将演示如何使用两个端点创建自定义路由,这些端点显示由Easy Digital Downloads(EDD)提供支持的电子商务站点中有关产品的信息。 此示例基于我为自己的网站构建的API插件。 如果您想在GitHub上查看完整的源代码或正在使用的API ,可以。

Although EDD does provide its own RESTful API, I wanted to expose the specific custom fields that I use on my own site. In my own implementation I also incorporate a second route called “docs,” which is wrapped around a custom post type that I use for documentation.

尽管EDD确实提供了自己的RESTful API,但我想公开我在自己的站点上使用的特定自定义字段。 在我自己的实现中,我还合并了第二条路线,称为“ docs”,该路线围绕着我用于文档的自定义帖子类型。

I might have been able to wrangle the EDD API or the core API’s custom post type and meta routes to do what I wanted, but for simplicity (and to have something that had exactly what I needed) I made my own routes and endpoints. It was quick, fun, and worked out great for the two places I’ve implemented it so far.

我也许可以缠结EDD API或核心API的自定义帖子类型和元路由来完成我想要的事情,但是为了简单起见(并拥有完全符合我需要的东西),我制作了自己的路由和端点。 到目前为止,这是快速,有趣的,并且对于我在这两个地方实施的效果都很好。

添加路线 (Adding Routes)

认识我最喜欢的新功能 (Meet My New Favorite Function)

Version two of the REST API introduces a new function called register_rest_route(). This lets you add a route to the REST API and pass in an array of endpoints. For each endpoint, you don’t just provide a callback function for responding to the request, but you can also define what fields you want in your query — which includes defaults, sanitation and validation callbacks, as well as a separate permissions callback.

REST API的第二版引入了一个称为register_rest_route()的新函数。 这使您可以添加到REST API的路由并传递一系列端点。 对于每个端点,您不仅可以提供用于响应请求的回调函数,还可以定义查询中想要的字段-包括默认,卫生和验证回调以及单独的权限回调。

There are additional features here that are still evolving, I recommend reading the class for the default posts routes. It is a great resource on how to use the REST API to query posts.

这里还有其他功能还在不断发展,我建议阅读该类以获取默认的发布路线。 它是有关如何使用REST API查询帖子的重要资源。

I’m going to focus on these three things: callback, field arguments, and permissions check. These three functions will illustrate how the architecture of the API works. They’re also really useful because once you get to your callback, you will have all of the fields you need, and they

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值