Download the PHP package craftcms/element-api without Composer

On this page you can find all versions of the php package craftcms/element-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package element-api

Element API for Craft CMS

This plugin makes it easy to create a JSON API for your entries (and other element types) in Craft CMS.

It’s powered by Phil Sturgeon’s excellent Fractal package.

Requirements

This plugin requires Craft CMS 4.3.0+ or 5.0.0+.

Installation

You can install this plugin from the Plugin Store or with Composer.

From the Plugin Store

Go to the Plugin Store in your project’s Control Panel and search for “Element API”. Then click on the “Install” button in its modal window.

With Composer

Open your terminal and run the following commands:

Setup

To define your API endpoints, create a new element-api.php file within your config/ folder. This file should return an array with an endpoints key, which defines your site’s API endpoints.

Within the endpoints array, keys are URL patterns, and values are functions that define the endpoint configurations.

Endpoint Configuration Settings

Endpoint configuration arrays can contain the following settings:

class

The class name of the Fractal resource that should be used to serve the request. If this isn’t set, it will default to craft\elementapi\resources\ElementResource. (All of the following configuration settings are specific to that default class.)

elementType (Required)

The class name of the element type that the API should be associated with. Craft’s built-in element type classes are:

`

criteria

An array of parameters that should be set on the Element Query that will be fetching the elements.

contentType

The content type the endpoint responses should have.

By default, the content type will be:

transformer

The transformer that should be used to define the data that should be returned for each element. If you don’t set this, the default transformer will be used, which includes all of the element’s direct attribute values, but no custom field values.

Your custom transformer class would look something like this:

one

Whether only the first matching element should be returned. This is set to false by default, meaning that all matching elements will be returned.

paginate

Whether the results should be paginated. This is set to true by default, meaning that only a subset of the matched elements will be included in each response, accompanied by additional metadata that describes pagination information.

elementsPerPage

The max number of elements that should be included in each page, if pagination is enabled. By default this is set to 100.

pageParam

The query string param name that should be used to identify which page is being requested. By default this is set to 'page'.

Note that it cannot be set to 'p' because that’s the parameter Craft uses to check the requested path.

resourceKey

The key that the elements should be nested under in the response data. By default this will be 'data'.

meta

Any custom meta values that should be included in the response data.

serializer

The serializer that should be used to format the returned data.

Possible values are:

includes

The include names that should be included for the current request, if any.

Note that this setting requires a custom transformer class that’s prepped to handle includes:

excludes

The include names that should be excluded for the current request, which would otherwise have been included (e.g. if they were listed as a default include), if any.

Like includes, this setting requires a custom transformer class.

callback

If this is set, a JSONP response will be returned with an application/javascript content type, using this setting value as the callback function.

For example, if you set this to:

Then the response will look like:

Note that if you set this, the jsonOptions and pretty settings will be ignored.

jsonOptions

The value of the $options argument that will be passed to json_encode() when preparing the response. By default JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE will be passed.

pretty

Shortcut for adding JSON_PRETTY_PRINT to jsonOptions.

cache

Whether the output should be cached, and for how long.

Possible values are:

Note that the onBeforeSendData event does not get triggered when the cache is warm.

cacheKey

The key that responses should be cached with, if something custom is needed.

Dynamic URL Patterns

URL patterns can contain dynamic subpatterns in the format of <subpatternName:regex>, where subpatternName is the name of the subpattern, and regex is a valid regular expression. For example, the URL pattern “news/<entryId:\d+>.json” will match URLs like news/100.json. You can also use the tokens {handle} and {slug} within your regular expression, which will be replaced with the appropriate regex patterns for matching handles and element slugs.

Any subpattern matches in the URL pattern will be mapped to the endpoint config function’s arguments. For example, if a URL pattern contains an entryId subpattern, then you can add an $entryId argument to your endpoint config function, and whatever matches the URL subpattern will be passed to $entryId.

Setting Default Configuration Settings

You can specify default values for your endpoint configuration settings by adding a defaults key alongside your endpoints key (not within it).

Examples

Here are a few endpoint examples, and what their response would look like.

Paginated Entry Index Endpoint

Single Entry Endpoint

JSON Feed

Here’s how to set up a JSON Feed (Version 1.1) for your site with Element API.

Note that photos, body, summary, and tags are imaginary custom fields.


All versions of element-api with dependencies

PHP Build Version
Package Version
Requires craftcms/cms Version ^4.3.0|^5.0.0-beta.1
league/fractal Version ^0.20.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package craftcms/element-api contains the following files

Loading the files please wait ....