|
1 | 1 | # DotCMS Symfony Integration |
2 | 2 |
|
3 | | -This project demonstrates how to integrate DotCMS with Symfony using the DotCMS PHP SDK. It provides a complete example of rendering DotCMS pages within a Symfony application, including layouts, containers, and content types. |
| 3 | +This project demonstrates how to build a DotCMS webapp with Symfony using the DotCMS PHP SDK. It provides a complete example of rendering DotCMS pages within a Symfony application, including layouts, containers, and content types. |
4 | 4 |
|
5 | 5 | ## Overview |
6 | 6 |
|
7 | 7 | This integration allows you to: |
8 | 8 |
|
9 | 9 | - Fetch and render DotCMS pages in a Symfony application |
10 | | -- Use Symfony's routing system to handle DotCMS URLs |
11 | | -- Render DotCMS containers and contentlets using Twig templates |
12 | | -- Handle DotCMS errors with Symfony's exception system |
| 10 | +- Integrate Symfony routing with DotCMS, enabeling content authors to create new pages without developer intervention. |
| 11 | +- Render DotCMS Pages using Twig templates, this includes layouts, containers and contentlets. |
| 12 | +- Integrating DotCMS SDK feedback into Symfony's exception framework for a smooth and reliable developer experience. |
13 | 13 |
|
14 | 14 | ## Prerequisites |
15 | 15 |
|
16 | 16 | - PHP 8.1 or higher |
17 | | -- Composer |
18 | | -- Symfony CLI (optional, for local development) |
19 | | -- DotCMS instance with API access |
| 17 | +- [Composer](https://getcomposer.org/doc/00-intro.md) |
| 18 | +- [Symfony CLI](https://symfony.com/download) |
| 19 | +- DotCMS instance with [API access](https://dev.dotcms.com/docs/rest-api-authentication) |
20 | 20 |
|
21 | 21 | ## Project Structure |
22 | 22 |
|
@@ -70,8 +70,12 @@ DOTCMS_API_KEY=your-api-key-here |
70 | 70 |
|
71 | 71 | ## Configuration |
72 | 72 |
|
| 73 | +All the configuration described below is already implemented in this example project. The following sections explain the key components and how they work together to integrate DotCMS with Symfony. |
| 74 | + |
73 | 75 | ### 1. Configure DotCMS Client |
74 | 76 |
|
| 77 | +Symfony have [Service Container](https://symfony.com/doc/current/service_container.html) allows you to centralize useful objects waiting to be used within the app. |
| 78 | + |
75 | 79 | In `config/services.yaml`, add the DotCMS client configuration: |
76 | 80 |
|
77 | 81 | ```yaml |
@@ -104,9 +108,13 @@ services: |
104 | 108 | tags: ['twig.extension'] |
105 | 109 | ``` |
106 | 110 |
|
| 111 | +In this case we register two objects from the SDK the dotCMS `Config` and the `DotCMSClient`. |
| 112 | + |
| 113 | +In Symfony's service container, we're initializing `DotCMSClient` by injecting a `Config` object as a dependency. The `Config` object itself is configured with environment variables and predefined options for the HTTP requests to the dotCMS APIs. |
| 114 | + |
107 | 115 | ### 2. Create a DotCMS Service |
108 | 116 |
|
109 | | -Create a service to wrap the DotCMS client in `src/Service/DotCMSService.php`: |
| 117 | +Create a service to wrap the PHP `DotCMSClient` in `src/Service/DotCMSService.php`: |
110 | 118 |
|
111 | 119 | ```php |
112 | 120 | <?php |
@@ -139,8 +147,16 @@ class DotCMSService |
139 | 147 | } |
140 | 148 | ``` |
141 | 149 |
|
| 150 | +The `DotCMSService` class serves two important purposes: |
| 151 | + |
| 152 | +1. **Dependency Injection**: Through Symfony's service container, the fully configured `DotCMSClient` is automatically injected into our service. |
| 153 | + |
| 154 | +2. **Facade Pattern**: The service acts as a facade, exposing only specific DotCMS client methods needed by the application (like `getPage()`). |
| 155 | + |
142 | 156 | ### 3. Configure Routes |
143 | 157 |
|
| 158 | +DotCMS allows content authors to create pages without developer intervention. To support this, we use a catch-all route that handles all page requests through a single controller. |
| 159 | + |
144 | 160 | In `config/routes.yaml`, add a catch-all route to handle DotCMS pages: |
145 | 161 |
|
146 | 162 | ```yaml |
@@ -224,6 +240,10 @@ class CatchAllController extends AbstractController |
224 | 240 | } |
225 | 241 | ``` |
226 | 242 |
|
| 243 | +This controller serves as the central entry point for all DotCMS page requests. It retrieves the current request path, fetches the corresponding page from DotCMS via the service, and renders it using the page template. |
| 244 | + |
| 245 | +The controller also handles error cases by mapping DotCMS exceptions to appropriate Symfony HTTP exceptions, ensuring proper error responses. |
| 246 | + |
227 | 247 | ### 5. Create Twig Extension |
228 | 248 |
|
229 | 249 | Create a Twig extension to help with rendering DotCMS content in `src/Twig/DotCMSExtension.php`: |
@@ -328,6 +348,8 @@ class DotCMSExtension extends AbstractExtension |
328 | 348 | } |
329 | 349 | ``` |
330 | 350 |
|
| 351 | +This Twig extension provides utility functions for rendering DotCMS content in templates - handling container data extraction, content-type HTML generation, and proper attribute formatting for DotCMS elements. |
| 352 | + |
331 | 353 | ### 6. Create Templates |
332 | 354 |
|
333 | 355 | Create the necessary Twig templates to render DotCMS content: |
@@ -438,6 +460,26 @@ symfony server:start |
438 | 460 | 4. The Twig extension provides helper functions for rendering DotCMS containers and content. |
439 | 461 | 5. Content-type specific templates render each content type appropriately. |
440 | 462 |
|
| 463 | +```mermaid |
| 464 | +sequenceDiagram |
| 465 | + participant User |
| 466 | + participant Symfony as Symfony Router |
| 467 | + participant Controller as CatchAllController |
| 468 | + participant Service as DotCMSService |
| 469 | + participant DotCMS as DotCMS API |
| 470 | + participant Twig as Twig Templates |
| 471 | + |
| 472 | + User->>Symfony: Request URL |
| 473 | + Symfony->>Controller: Route to show() method |
| 474 | + Controller->>Service: getPage(path) |
| 475 | + Service->>DotCMS: API Request |
| 476 | + DotCMS-->>Service: Return PageAsset |
| 477 | + Service-->>Controller: Return PageAsset |
| 478 | + Controller->>Twig: Render with page data |
| 479 | + Twig->>Twig: Process with Twig Extensions |
| 480 | + Twig-->>User: Return rendered HTML |
| 481 | +``` |
| 482 | + |
441 | 483 | ## Error Handling |
442 | 484 |
|
443 | 485 | The application includes comprehensive error handling: |
|
0 commit comments