Make queries with API endpoints

Hello.

So far, I understand that the recommended way to construct API endpoints is to use twig templates. In the template, you can instantiate a collection based in the content type you previously defined in the Tailor blueprint.

What I want to accomplish now is to control the response, by means of use URL parameters. For example, given this code:

title = “Blog Posts”

layout = “default”

url = “/api/blogposts/:status?/”

[collection blogposts]

handle = “Content\BlogPost”

{% set result = collect() %}

{% for post in blogposts %}
{% do result.push({
id: post.id,
title: post.title,
email: post.author.email,
created_at: post.created_at,
updated_at: post.updated_at,
status: post.status
}) %}
{% endfor %}

{% do response(result) %}

How to select only the records that match the post ‘status’ passed in the URL, or for that matter, any other URL parameter combination?

Thanks in advance.

Hi @jeraso

The blogposts component supports building queries, so you can pass in the status parameter via this.param

{% set posts = blogposts.where('status', this.param.status).get() %}

Here is the complete example:

title = "Blog Posts"
layout = "default"
url = "/api/blogposts/:status?/"

[collection blogposts]
handle = "Content\BlogPost"
==
{% set result = collect() %}

{% set posts = blogposts.where('status', this.param.status).get() %}

{% for post in posts %}
    {% do result.push({
        id: post.id,
        title: post.title,
        email: post.author.email,
        created_at: post.created_at,
        updated_at: post.updated_at,
        status: post.status
    }) %}
{% endfor %}

{% do response(result) %}

Here is a link to the documentation for the query builder:

1 Like

Is this actually the recommended way of building API endpoints?

I don’t really understand why you would use this way over defining them in a routes file and pointing to controllers.

Are there any resources anywhere that explain why you’d do this with twig and what the benefits are?

Or is it just a case of, here’s another way of doing something?

1 Like

Thank you. It is easier than I though!

Best regards.

I’m not an expert in the subject, but if we are using October CMS it is because we need to build CMS applications in a more agile and convenient way. So, given that the existing plugins have limitations, I think that the Tailor/Twig method is more adequate, since it is integrated in the core.
Of course, you could construct your API following the “Laravel way”, but it will take a lot more of time and effort, and then integrating October CMS will be overkill, IMO.

Best regards.

1 Like

Hi @neil-impelling

Yes, sure, the documentation describes the purpose, and it is comparable to Laravel’s API resources approach:

It is another way to do it:

  • supported by the Editor, where you don’t need access to PHP files
  • full access to CMS components and AJAX handlers (including Tailor)
  • preferable since the view layer is self-documenting
1 Like

To add a simplified version:

title = "Blog Posts"
layout = "default"
url = "/api/blogposts/:status?/"

[collection]
handle = "Content\BlogPost"
==
{% set posts = collection.where('status', this.param.status).get() %}

{% do response({
    data: posts
}) %}

A simplified version with pagination:

title = "Blog Posts"
layout = "default"
url = "/api/blogposts/:status?/"

[collection]
handle = "Content\BlogPost"
==
{% set posts = collection.where('status', this.param.status).paginate() %}
{% set pager = pager(posts) %}

{% do response({
    data: posts,
    links: pager.links,
    meta: pager.meta
}) %}
1 Like