Download the PHP package getkirby/kql without Composer
On this page you can find all versions of the php package getkirby/kql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package kql
Kirby QL
Kirby's Query Language API combines the flexibility of Kirby's data structures, the power of GraphQL and the simplicity of REST.
The Kirby QL API takes POST requests with standard JSON objects and returns highly customized results that fit your application.
Playground
You can play in our KQL sandbox. The sandbox is based on the Kirby starterkit.
âšī¸ Source code of the playground is available on GitHub.
Example
Given a POST request to: /api/query
đ Response
Installation
Manual
Download and copy this repository to /site/plugins/kql
of your Kirby installation.
Composer
Documentation
API Endpoint
KQL adds a new query
API endpoint to your Kirby API (i.e. yoursite.com/api/query
). This endpoint requires authentication.
You can switch off authentication in your config at your own risk:
Sending POST Requests
You can use any HTTP request library in your language of choice to make regular POST requests to your /api/query
endpoint. In this example, we are using the fetch
API and JavaScript to retrieve data from our Kirby installation.
query
With the query, you can fetch data from anywhere in your Kirby site. You can query fields, pages, files, users, languages, roles and more.
Queries Without Selects
When you don't pass the select option, Kirby will try to come up with the most useful result set for you. This is great for simple queries.
Fetching the Site Title
đ Response
Fetching a List of Page IDs
đ Response
Running Field Methods
Queries can even execute field methods.
đ Response
select
KQL becomes really powerful by its flexible way to control the result set with the select option.
Select Single Properties and Fields
To include a property or field in your results, list them as an array. Check out our reference for available properties for pages, users, files, etc.
đ Response
You can also use the object notation and pass true for each key/property you want to include.
đ Response
Using Queries for Properties and Fields
Instead of passing true, you can also pass a string query to specify what you want to return for each key in your select object.
đ Response
Executing Field Methods
đ Response
Creating Aliases
String queries are a perfect way to create aliases or return variations of the same field or property multiple times.
đ Response
Subqueries
With such string queries you can of course also include nested data
đ Response
Subqueries With Selects
You can also pass an object with a query
and a select
option
đ Response
Pagination
Whenever you query a collection (pages, files, users, roles, languages) you can limit the resultset and also paginate through entries. You've probably already seen the pagination object in the results above. It is included in all results for collections, even if you didn't specify any pagination settings.
limit
You can specify a custom limit with the limit option. The default limit for collections is 100 entries.
đ Response
page
You can jump to any page in the resultset with the page
option.
đ Response
Pagination in Subqueries
Pagination settings also work for subqueries.
Multiple Queries in a Single Call
With the power of selects and subqueries you can basically query the entire site in a single request
Allowing Methods
KQL is very strict with allowed methods by default. Custom page methods, file methods or model methods are not allowed to make sure you don't miss an important security issue by accident. You can allow additional methods though.
Allow List
The most straight forward way is to define allowed methods in your config.
DocBlock Comment
You can also add a comment to your methods' doc blocks to allow them:
This works for model methods as well as for custom page methods, file methods or other methods defined in plugins.
Blocking Methods
You can block individual class methods that would normally be accessible by listing them in your config:
Blocking Classes
Sometimes you might want to reduce access to various parts of the system. This can be done by blocking individual methods (see above) or by blocking entire classes.
Now, access to any user is blocked.
Custom Classes and Interceptors
If you want to add support for a custom class or a class in Kirby's source that is not supported yet, you can list your own interceptors in your config
You can put the class for such a custom interceptor in a plugin for example.
Interceptor classes are pretty straight forward. With the CLASS_ALIAS
you can give objects with that class a short name for KQL queries. The $toArray
property lists all methods that should be rendered if you don't run a subquery. I.e. in this case kirby.system
would render an array with the isInstallable
value.
The allowedMethods
method must return an array of all methods that can be access for this object. In addition to that you can also create your own custom methods in an interceptor that will then become available in KQL.
This custom method can now be used with kirby.system.isReady
in KQL and will return yes it is!
Unintercepted Classes
If you want to fully allow access to an entire class without putting an interceptor in between, you can add the class to the allow list in your config:
This will introduce full access to all public class methods. This can be very risky though and you should avoid this if possible.
No Mutations
KQL only offers access to data in your site. It does not support any mutations. All destructive methods are blocked and cannot be accessed in queries.
Plugins
What's Kirby?
- getkirby.com â Get to know the CMS.
- Try it â Take a test ride with our online demo. Or download one of our kits to get started.
- Documentation â Read the official guide, reference and cookbook recipes.
- Issues â Report bugs and other problems.
- Feedback â You have an idea for Kirby? Share it.
- Forum â Whenever you get stuck, don't hesitate to reach out for questions and support.
- Discord â Hang out and meet the community.
- Mastodon â Spread the word.
- Instagram â Share your creations: #madewithkirby.
License
Bastian Allgeier