Public PHP API¶
The Public PHP API enables you to interact with eZ Platform's Repository and content model from your PHP code.
You can use it to create, read, update, manage, and delete all objects available in eZ Platform, namely content and related objects such as Sections, Locations, Content Types, languages, etc.
The PHP API is built on top of a layered architecture, including a persistence SPI that abstracts storage. Using the API ensures that your code will be forward compatible with future releases based on other storage engines.
For more information see a presentation about eZ Platform API.
Using API services¶
You can access the PHP API by injecting relevant services into your code.
The API provides access to Content, User, Content Types and other features through various services.
Those services are obtained using
The full list of available services covers:
The services provide interaction with read-only value objects from the
Those objects are divided into sub-namespaces, such as
Each sub-namespace contains a set of value objects,
Value objects come with their own properties, such as
as well as with methods that provide access to more related information,
Creating and updating objects¶
Value objects fetch data from the Repository and are read-only.
To create and modify Repository values you need to use structs, such as
Value info objects¶
Some complex value objects have an
These objects provide you with lower-level information.
Content enables you to retrieve Fields, Content Type, or previous versions.
One of the responsibilities of the Repository is user authentication. Every action is executed as a user.
When using the PHP API, authentication is performed in three ways:
Back Office authentication¶
When actions are performed through the Back Office, they are executed as the logged-in User. This User's permissions will affect the behavior of the Repository. The User may, for example, not be allowed to create content, or view a particular Section.
To skip permission checks, you can use the
It allows API execution to be performed with full access, sand-boxed.
You can use this method to perform an action that the current User does not have permissions for.
For example, to hide a Location, use:
1 2 3 4 5 6 7
Setting the Repository user¶
In a command line script, the Repository runs as if executed by the anonymous user.
sudo() is the recommended option,
you can also set the current user to a user with necessary permissions to achieve the same effect.
In order to identify as a different user, you need to use the
UserService together with
(in the example
admin is the login of the administrator user):
eZ\Publish\API\Repository\PermissionService can be injected to have a Service which provides both
PermissionCriterionResolver. It supports auto-wiring.
This is not required in template functions or controller code, as the HTTP layer takes care of identifying the user, and automatically sets it in the repository.
If you want to identify a user with their credentials instead, provide them in the following way:
PHP API uses Exceptions to handle errors. Each API method may throw different exceptions, depending on what it does.
It is good practice to cover every exception you expect to happen.
For example if you are using a command which takes the Content ID as a parameter, the ID may either not exist, or the referenced Content item may not be visible to the user.
Both cases should be covered with error messages:
1 2 3 4 5 6 7