You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -9,75 +9,179 @@ Using modern pages, you will have access to the PrestaShop debug toolbar, the se
9
9
10
10
## How to declare a new Controller
11
11
12
-
Somewhere in your module declare a new class that will act as a Controller:
12
+
{{% notice warning %}}
13
+
**PrestaShop 9.0+**: The `FrameworkBundleAdminController` is **deprecated** and will be removed in PrestaShop 10.0. Use `PrestaShopAdminController` instead for new controllers.
14
+
{{% /notice %}}
15
+
16
+
In your module, create a new controller class in the `src/Controller/` directory:
See the complete list in the [PrestaShopAdminController source code](https://github.com/PrestaShop/PrestaShop/blob/9.0.0/src/PrestaShopBundle/Controller/Admin/PrestaShopAdminController.php).
115
+
{{% /notice %}}
116
+
117
+
{{% notice warning %}}
118
+
**Doctrine repositories must be injected:**
119
+
120
+
Doctrine ORM is not directly accessible via `$this->getDoctrine()` or similar methods. All Doctrine repositories and entity managers must be injected as dependencies in your controller's constructor or action methods.
One of the two service configuration options above is **essential and required** for your controller to work properly. Without this configuration:
168
+
- **The controller will not function** and will throw errors
169
+
- Helper methods from `PrestaShopAdminController` will not be accessible
170
+
- Services like Twig, Form, Router, and other Symfony components will not be available
70
171
71
-
You have access to the Container, to Twig as rendering engine, the Doctrine ORM, everything from Symfony framework ecosystem.
72
-
Note that you must return a `Response` object, but this can be a `JsonResponse` if you plan to make a single page application (or "SPA").
172
+
**Important:** The service name (e.g., `MyModule\Controller\DemoController`) **must exactly match** the fully qualified class name (FQCN) of your controller. If the service name doesn't match the class name, Symfony and PrestaShop will not be able to identify and instantiate the controller, resulting in errors.
73
173
74
-
{{% notice note %}}
75
-
This controller works exactly the same as the Core Back Office ones.
76
-
{{% /notice %}}
174
+
**Understanding the configuration:**
175
+
- `autowire: true` - Automatically injects services in constructors and method parameters
176
+
- `autoconfigure: true` - Automatically configures the controller as a service and enables all controller features
177
+
- `controller.service_arguments`tag - Required if your controller doesn't extend `AbstractController` to enable method parameter injection
178
+
{{% /notice %}}
179
+
180
+
## Autoloading Configuration
77
181
78
182
You must enable the autoloading for this Controller. For example using a `composer.json` file for your module.
79
183
80
-
####Example using PSR-4 namespacing
184
+
### Example using PSR-4 namespacing
81
185
82
186
1. Use namespace for your Controller file
83
187
@@ -87,7 +191,7 @@ You must enable the autoloading for this Controller. For example using a `compos
87
191
88
192
namespace MyModule\Controller;
89
193
90
-
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
194
+
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
91
195
```
92
196
93
197
2. Configure composer to autoload this namespace
@@ -98,7 +202,7 @@ You must enable the autoloading for this Controller. For example using a `compos
98
202
"description": "...",
99
203
"autoload": {
100
204
"psr-4": {
101
-
"MyModule\\Controller\\": "src/Controller/"
205
+
"MyModule\\": "src/"
102
206
}
103
207
},
104
208
"config": {
@@ -182,6 +286,48 @@ In order to generate the valid URI of a controller you created from inside the m
182
286
```
183
287
184
288
289
+
## Key Changes in PrestaShop 9.0 (Symfony 6.4)
290
+
291
+
### Controllers as Services
292
+
293
+
In Symfony 6.4, controllers must be defined as services. The container passed to controllers is no longer the "global container" but a dedicated, optimized container based on the services injected into it.
294
+
295
+
**Important implications:**
296
+
297
+
- The `$this->get('service_name')` method is no longer available in modern controllers
298
+
- `FrameworkBundleAdminController`maintains backward compatibility but is **deprecated** and will be removed in PrestaShop 10.0
299
+
- New controllers should extend `PrestaShopAdminController` which follows Symfony best practices
300
+
301
+
### Dependency Injection Methods
302
+
303
+
You have three main ways to inject services:
304
+
305
+
1. **Constructor injection** - For services used across multiple actions
306
+
2. **Method injection** - For services used in a single action (more optimized)
307
+
3. **Autowire attribute** - Use `#[Autowire(service: 'service_id')]` to inject services by their ID
308
+
309
+
### PrestaShopAdminController Helper Methods
310
+
311
+
The new base controller provides convenient helper methods for commonly used services:
312
+
313
+
- `$this->getConfiguration()`- Access configuration service
314
+
- `$this->getTranslator()`- Access translator service
315
+
- `$this->getRouter()`- Access router service
316
+
- `$this->getFlashBag()`- Access flash messages
317
+
318
+
See the [full list of helper methods](https://github.com/PrestaShop/PrestaShop/blob/9.0.0/src/PrestaShopBundle/Controller/Admin/PrestaShopAdminController.php) in the class.
319
+
320
+
{{% notice info %}}
321
+
**Learn more about Symfony controllers:**
322
+
- [Controllers as services](https://symfony.com/doc/6.4/controller/service.html)
It is safer to define permissions required to use your controller, this can be configured using the `#[AdminSecurity]` attribute and some configuration in your routing file. You can read this documentation if you need more details about [Controller Security]({{< ref "/9/development/architecture/migration-guide/controller-routing.md#security" >}}).
330
+
331
+
{{% notice note %}}
332
+
**PrestaShop 9.0+**: The `@AdminSecurity` annotation has been replaced with the `#[AdminSecurity]` attribute following PHP 8 standards.
0 commit comments