تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Enabling REST API0:00

Let's talk about using Statamic in a headless fashion. You don't have to use Statamic's frontend, backend, control panel, everything all together. You can use just the control panel to manage your content, and then either use the REST API or GraphQL to pull your data out into any other application, any other frontend stack, native, mobile, you name it. Let's walk through how that works. First the REST API. In your config/statamic/api.php file, take a look at a couple of settings that you have. First you need to enable it, which we can do through the .env file, or we can just set this to be true.

First you need to enable it, which we can do through the .env file, or we can just set this to be true. Then you have your different resource types, and you can enable or disable which ones, that way you're not leaking any other information you don't want out there into your API. So for the sake of example, we'll turn on collections and maybe taxonomies. Next you can customize your route, by default it's /api. And finally, come down here to caching, and we'll just disable the cache for development purposes, that way we can see anything that changes. It is worth mentioning that the API, the REST API, and GraphQL are both pro features which require a license of Statamic to use.

Testing REST Endpoints1:16

It is worth mentioning that the API, the REST API, and GraphQL are both pro features which require a license of Statamic to use. Next let's open an HTTP client, in this case I'll be using HTTPPi. Let's go to the endpoint, API/collections/news, and let's get the entries. You can see all that data is returned, you can also get pages. And if you want, you can also get specific entries if you know the ID. That's all the data for the single entry. The API also supports filtering, so you can pass a query string, filter, and then it's field name, so let's try title contains flux, and you can also specify the fields that you want returned.

field name, so let's try title contains flux, and you can also specify the fields that you want returned. Let's just look at the title, now we can see we have this entry, and this one, which are the only entries with the word flux in the title. You can also comma separate which fields you want included, if you want a nice small tight little package. Things work pretty similarly, taxonomies, tags, terms, and here are our taxonomies, and so on. You can use our documentation and go through all the different endpoints that we have available from taxonomies, to navtrees, users, assets, and so on.

Enabling GraphQL Explorer3:01

You can use our documentation and go through all the different endpoints that we have available from taxonomies, to navtrees, users, assets, and so on. Now if you want to use GraphQL instead of REST API, you can use that as well. Enable it in your config file, choose which resources you want to enable, disable the cache for dev purposes, and then if you head to the control panel, now that we've enabled it, we have this GraphQL explorer, where you can take a look at all of the different queries that are available to you. And let's say we want to do entries from the news collection, we want three entries, we want to include the data, the ID, maybe the lastModified title. GraphQL has its own nuances to it, so these are interfaces which allow you to get the

Querying Blueprint Fields3:49

want to include the data, the ID, maybe the last modified title. GraphQL has its own nuances to it, so these are interfaces which allow you to get the fields from the blueprints. So the format here is the interface name, so entry, and then it's the collection name, and then it's the blueprint name. So in our case, we have the news blueprint inside the news collection, and that's going to give us all the fields from our entries. So we can have an article content, and you're going to want to include the sets inside it because BARD has multiple fields and sets configured inside. We'll include the date, we format it as your month day, whether it's been published, give

Headless ModeREST APIGraphQL

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟