PHP in Sulu: The power of code
When evaluating content management systems (CMS), finding the right balance between flexibility and simplicity can be tricky. Some CMSs don’t allow any custom code at all, others require a good dose of custom code to do anything useful. Neither of these opposite poles is a suitable foundation for a CMS that should be user-friendly and admin-friendly while also being highly flexible.
Sulu occupies the middle ground between these extremes — not by compromising but by including the best of both worlds.
If you want to learn more about the possibilities of Sulu CMS, contact us for a demo.
Sulu CMS is a true Symfony application
If you know Symfony, you already know the foundation of Sulu. Sulu is a Symfony application and makes generous use of Symfony’s standard configuration options, whether it’s YAML files, XML documents, or PHP Config files. Sulu is configurable without having to write custom code, by leveraging XML to set up page structures and Twig templates to define the visual representation of page contents.
In fact, if you work through The Book (to create a Sulu-based website from scratch), you will see the first PHP code snippet in the final section, “Extending the admin UI.” You can indeed get quite far in Sulu without writing PHP code.
When do you need to write PHP for Sulu?
A good CMS system won’t get in your way if you want to add custom functionality, and the most flexible way of customizing is to write code. Use cases for writing PHP code in Sulu include:
- Adding Sulu as an add-on CMS to another Symfony application
- Connecting to third-party applications and services
- Adding custom business logic
Sulu as a CMS for a Symfony application
A stand-alone CMS is fine for blogs or small pages, but a CMS connected to business applications is a really powerful combination. If your applications are built with Symfony, adding Sulu is a breeze. Being a Symfony application itself, it naturally combines with other Symfony apps, thanks to Symfony’s bundle concept. What you then need is custom glue code to get data from your applications to Sulu or vice versa.
For example, Allianz Cinema uses Sulu as a Web frontend for their ticketing system. Their web visitors can search for upcoming events, read the details, and then book a ticket without changing to another system — everything from ticket selection to printing the ticket as a PDF is fully integrated with Sulu.
Or imagine you want to combine a huge database of recipes with a lightning-fast web frontend like Küchengötter.de did. Thanks to Sulu being a Symfony app, the investment is safe because, as Küchengötter points out, “we’re free to use our data with other PHP applications in the future without completely rebuilding everything.”
Connect Sulu with third-party applications and services
Even for non-Symfony applications, there are ways of connecting to their services and data:
- Write a custom database connector to fetch data from the application’s database
- Write an API client if the application exposes an API, whether it is a REST, GraphQL, gRPC, or any other kind of API
- Use a messaging bus like the Sulu Messenger for communicating with applications asynchronously
Add custom business logic to Sulu
Besides integrating with other applications, Sulu can be extended with custom business logic. A typical starting point is a custom controller that can add new behavior to a data model and its representation in the UI. This is the most straightforward way of fine-tuning the CMS to your specific business needs and workflows.
Likewise, you can extend Sulu’s data model by adding custom entities or extending existing ones.
Where can you add PHP code in Sulu?
Adding custom entities
Entities in Sulu are standard Doctrine entities. The Sulu Workshop project contains an example of adding an entity. In this example, a project requires a custom location entity for managing location-based events. The command make:entity
generates a new entity class based on your needs.
Adding custom controllers
While custom entities expand the range of data types, custom controllers are the primary place for implementing custom business functionality of all kinds inside Sulu. Sitting between a data model and a view, a controller can deliver dynamic content to the user interface (UI), process custom forms, exchange data with external services, and more.
Controllers are a central part of extending Sulu with custom functionality. To get started, read the article PHP in Sulu: How To Write A Custom Controller To Render Any Content or check out this recipe from our cookbook.
Integrating with third-party services
Sulu enables developers to connect to third-party applications and services in various ways: by calling out to an API or triggering a webhook, by tapping into Sulu’s event model, or by utilizing message queues.
- External APIs and webhooks: Accessing an external REST API or triggering a remote webhook is straightforward. Symfony’s HttpClient component provides the functionality required for consuming APIs. An alternative to HttpClient is the Webhook component in combination with a RemoteEvent consumer.
- Event dispatchers and listeners: Sulu uses Symfony’s event model for triggering and acting on events. Events can originate from the Symfony kernel, from third-party bundles, or from Sulu admin tasks. (See the next section for details.) Events that implement the
DomainEvent
class can be recorded in the activity log, to be viewed in the UI. - Message queues: The Sulu Messenger is a Symfony message bus that provides additional middleware but is otherwise compatible with the standard Symfony Messenger.
Processing events from Symfony, Doctrine, or Sulu
In Sulu, events can occur on various levels:
- Symfony dispatches events when handling an HTTP request. They occur at various stages (such as before or after a controller is called) and allow for modifying controller arguments, transforming data, and more.
- The Doctrine ORM layer dispatches several events related to objects' lifecycles, such as PrePersist, PostPersist, PreUpdate, or PostUpdate events.
- The Sulu Document Manager, a domain abstraction of the database layer, is highly event-driven.
- Sulu’s own content storage (an upcoming feature at the time of this writing) will be driven by normal domain events.
- Finally, Sulu emits events for admin and domain activities, such as “an article has been published”.
These event sources add a range of possibilities to react to changes and updates in the system.
Extending the admin UI
The admin UI is quite a unique selling point of Sulu CMS.
The admin UI gives content managers control over the page structure without coding. Content blocks like text, images, dates, pick fields, and more have a configuration interface in the admin UI. If developers add new content types, they can extend the admin UI accordingly to include the new content types. Without the admin UI and its out-of-the-box extensibility, developers would have to write configuration interfaces for their custom components from scratch.
Conclusion: Everything is possible
It is entirely possible to create a complete website with Sulu CMS without touching any PHP code. But Sulu’s highly modular and open architecture shines when you want to add custom business logic and integrate Sulu with other systems. On top of this flexibility and openness, you don’t need to learn a proprietary language or get familiar with an unknown ecosystem. PHP and Symfony are all you need to extend Sulu beyond its already generous configurability.
If you have questions, get in touch. We’re happy to talk to you.