Download the PHP package sulu/headless-bundle without Composer
On this page you can find all versions of the php package sulu/headless-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sulu/headless-bundle
More information about sulu/headless-bundle
Files in sulu/headless-bundle
Package headless-bundle
Short Description Bundle that provides controllers and services for using Sulu as headless content management system
License MIT
Informations about the package headless-bundle
SuluHeadlessBundle
The SuluHeadlessBundle provides controllers and services for using the Sulu content management system in a headless way.
To achieve this, the bundle includes a controller that allows to retrieve the content of a Sulu page as plain JSON content. Furthermore, the bundle provides APIs for accessing features that are available via Twig extensions in traditional templates such as navigation contexts and snippet areas. Finally, the bundle includes an optional single page application setup that is built upon React and MobX and utilizes the functionality of the bundle.
The SuluHeadlessBundle is compatible with Sulu starting from version 2.0. Have a look at the require
section in
the composer.json to find an
up-to-date list of the requirements of the bundle.
Please be aware that this bundle is still under development and might not cover every use-case yet.
Depending on the feedback of the community, future versions of the bundle might contain breaking changes.
🚀 Installation and Usage
Install the bundle
Execute the following composer command to add the bundle to the dependencies of your project:
Enable the bundle
Enable the bundle by adding it to the list of registered bundles in the config/bundles.php
file of your project:
Include the routes of the bundle
Include the routes of the bundle in a new config/routes/sulu_headless_website.yml
file in your project:
Set the controller of you template
To provide an API for retrieving the content of a page in the JSON format, the controller of the page template
must be set to the HeadlessWebsiteController
included in this bundle:
This controller will provide the content of the page as JSON object if the page is requested in the JSON format
via {pageUrl}.json
.
💡 Key Concepts
Deliver content of pages with the HeadlessWebsiteController
The main use-case of the SuluHeadlessBundle is delivering the content of a page as a JSON object. This can be
enabled individually per template by setting the controller of the template of the page
to Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction
. When using the HeadlessWebsiteController
as controller for a template, the content of the page is available as JSON object via {pageUrl}.json
.
Additionally to the content of the page, the JSON object returned by the HeadlessWebsiteController
contains meta
information such as the page template and the data of the page excerpt:
If the content of a page that uses the HeadlessWebsiteController
is requested without the .json
suffix, the
controller will render Twig template that is set as view
of the template of the page.
In this case, the data that would have been returned in case of a .json
request is available in the twig
template via a headless
variable.
This behaviour is compatible with the default Sulu WebsiteController
and allows to start a javascript application
that utilizes the functionality of the SuluHeadlessBundle after the initial request of the user.
Resolve content data to scalar values via ContentTypeResolver
Internally, Sulu uses ContentType
services that are responsible for persisting page content when a page is modified
and resolving the data that is passed to the Twig template when a page is rendered. Unfortunately, some ContentType
services pass non-scalar values such as media entities to the Twig template. As a JSON object must contain only scalar
values, the SuluHeadlessBundle cannot use the existing ContentType
services for resolving the content of a page.
To solve this problem, the SuluHeadlessBundle introduces ContentTypeResolver
services to resolve the content of
pages to scalar values. The bundle already includes ContentTypeResolver
services for various content types.
If your project includes custom content types or if you are not satisfied with an existing ContentTypeResolver
,
you can register your own ContentTypeResolver
by implementing the ContentTypeResolverInterface
and
adding a sulu_headless.content_type_resolver
tag to the service.
Provide popular Sulu functionality via JSON APIs
The Sulu content management system comes with various services and Twig extensions to simplify the development and the rendering complex websites. This functionality is not available when serving the content of the website in a headless way, therefore the SuluHeadlessBundle includes controllers to provide JSON APIs for accessing these features.
The APIs are registered as portal URLs and therefore their path is prefixed with the URL of the webspace. If you have configured a language-specific URL for your webspace, the API URL will look something like this:
https://example.org/en/api/...
Navigation
/api/navigations/{contextKey}
Parameter | Type | Default Value | Description |
---|---|---|---|
depth | integer | 1 |
Maximum depth of the navigation tree that is loaded. |
flat | boolean | false |
Return navigation as flat list instead of tree. |
excerpt | boolean | false |
Include excerpt data in the returned navigation. |
Example: /api/navigations/main?depth=2&flat=false&excerpt=true
Search
/api/search?q={searchTerm}
Parameter | Type | Default Value | Description |
---|---|---|---|
q | string | The text you want to search for. |
Example: /api/search?q=CMS
Snippet Areas
/api/snippet-areas/{area}
Parameter | Type | Default Value | Description |
---|---|---|---|
includeExtension | boolean | false |
Include extension data (e.g. excerpt) in the returned result. |
Example: /api/snippet-areas/settings?includeExtension=true
Reference single page application implementation
The SuluHeadlessBundle is completely frontend independent and does not require the use of a specific technology or
framework. Still, the bundle contains an independent and optional single page application setup in the
Resources/js-website
directory that allows you to quick-start your project and serves as a reference implementation
for utilizing the bundle functionality.
The provided reference implementation builds upon React as rendering library and utilizes MobX for state
management. It is built around a central viewRegistry
singleton that allows you to register React components
as view for specific types of resources (eg. pages of a specific template). The application contains a router that will
intercept the navigation of the browser, load the JSON data for the requested resource and render the respective view
with the loaded data.
To use the provided single page application setup, you need to include the following lines in your Twig template to initialize and start the application:
Additionally, you need to add the following files to your project to setup the single page application:
assets/headless/package.json
assets/headless/webpack.config.js
assets/headless/babel.config.js
assets/headless/src/index.js
assets/headless/src/views/HeadlessTemplatePage.js
Finally, you can build your frontend application by executing npm install
and npm run build
in the assets/headless
directory.
❤️ Support and Contributions
The Sulu content management system is a community-driven open source project backed by various partner companies. We are committed to a fully transparent development process and highly appreciate any contributions.
In case you have questions, we are happy to welcome you in our official Slack channel. If you found a bug or miss a specific feature, feel free to file a new issue with a respective title and description on the the sulu/SuluHeadlessBundle repository.
📘 License
The Sulu content management system is released under the under terms of the MIT License.
All versions of headless-bundle with dependencies
sulu/sulu Version ^2.4 || ^2.5@dev
symfony/config Version ^4.4 || ^5.4 || ^6.3 || ^7.0
symfony/dependency-injection Version ^4.4 || ^5.4 || ^6.3 || ^7.0
symfony/framework-bundle Version ^4.4 || ^5.4 || ^6.3 || ^7.0
symfony/http-foundation Version ^4.4 || ^5.4 || ^6.3 || ^7.0
symfony/http-kernel Version ^4.4 || ^5.4 || ^6.3 || ^7.0