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
Copy file name to clipboardExpand all lines: plugin/registration.md
+26-8Lines changed: 26 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,7 +128,7 @@ The default author code offered by the Marketplace consists of the author first
128
128
129
129
## Registration file
130
130
131
-
The **Plugin.php** file, called the *Plugin registration file*, is an initialization script that declares a plugin's core functions and information. This file is read in the boot process of Winter CMS when determining available plugins. Registration files can provide the following:
131
+
The **Plugin.php** file, called the _Plugin registration file_, is an initialization script that declares a plugin's core functions and information. This file is read in the boot process of Winter CMS when determining available plugins. Registration files can provide the following:
132
132
133
133
1. Information about the plugin, its name, and author.
134
134
1. Registration methods for extending the CMS and stating the intentions of the plugin.
@@ -199,24 +199,42 @@ Key | Description
199
199
200
200
## Routing and initialization
201
201
202
-
Plugin registration files can contain two methods: `boot` and `register`. These methods are run at separate points in the Winter CMS boot process.
202
+
Plugin registration files may include two optional methods: `register()` and `boot()`. These methods are executed at different stages during the Winter CMS boot process and serve distinct purposes.
203
203
204
-
The `register` method is called immediately when the plugin is found and the Plugin registration file is read. It can be used to register or provide global services, define functionality within the underlying framework or initialize functionality for the plugin.
204
+
### `register()`
205
+
206
+
This method is called **early** in the application's lifecycle—before other plugins and most services are fully initialized. Use it for:
207
+
208
+
- Binding services or singletons to the Laravel container.
209
+
- Registering custom service providers.
210
+
- Defining low-level functionality that does not depend on other plugins or CMS services.
211
+
212
+
**Example:**
205
213
206
214
```php
207
215
public function register()
208
216
{
209
-
App::register(MyProviderClass::class, function ($app) {
210
-
return new MyProviderClass();
217
+
App::bind('acme.myplugin.service', function () {
218
+
return new \Acme\MyPlugin\Services\MyService();
211
219
});
212
220
}
213
221
```
214
222
215
223
> **NOTE:** The `register` method is run very early on in the Winter CMS boot process, and some functions within Winter CMS or Laravel may not be available at that stage, especially in respect to third-party plugins or services. You should use the `boot` method for any functionality that is dependent on third-party plugins or services.
216
224
217
-
The `boot` method is called after all services are loaded and all plugins are registered. This method should be used to define functionality that is to be run on each page load, such as extending plugins or attaching to events.
225
+
### `boot()`
226
+
227
+
This method is called after all services are loaded and all plugins are registered. Use it for:
228
+
229
+
- Extending models, backend controllers, or form widgets.
230
+
- Listening to events or interacting with other plugins.
231
+
- Adding logic that depends on the full CMS context.
232
+
233
+
**Example:**
218
234
219
235
```php
236
+
use Backend\Models\User;
237
+
220
238
public function boot()
221
239
{
222
240
User::extend(function ($model) {
@@ -225,7 +243,7 @@ public function boot()
225
243
}
226
244
```
227
245
228
-
The `boot` and `register` methods are not called during the update process, or within some critical Backend sections and command-line tools, to protect the system from critical errors. To overcome this limitation, use [elevated permissions](#elevated-permissions).
246
+
> **NOTE:**The `boot()` and `register()` methods are not executed during critical backend operations or CLI update commands, unless the plugin has elevated permissions. See [elevated permissions](#elevated-permissions) for more details.
229
247
230
248
Plugins can also supply a file named **routes.php** that may contain custom routing logic, as defined in the [router service](../services/router). For example:
231
249
@@ -265,7 +283,7 @@ Custom Twig filters and functions can be registered in the CMS with the `registe
265
283
266
284
[Twig options](https://twig.symfony.com/doc/2.x/advanced.html#filters) are also able to be passed to change the behavior of the registered filters & functions by providing an array with an `'options'` element containing the options to be passed at time of registration where the callable value would be provided normally. If options are provided, then the callable handler for the filter / function being registered must either be present in a `'callable'` element or as the first element of the array.
267
285
268
-
>**IMPORTANT:** All custom Twig filters & functions registered via the `MarkupManager` (i.e.`registerMarkupTags()` will have the `is_safe` option set to `['html']` by default, which means that Twig's automatic escaping is disabled by default (effectively it's as if the `| raw` filter was always located after your filter or function's output) unless you provide the `is_safe` option during registration (`'options' => ['is_safe' => []]`).
286
+
>**IMPORTANT:** All custom Twig filters and functions registered via the `MarkupManager` (e.g.`registerMarkupTags()`) have the `is_safe` option set to `['html']` by default. This disables Twig's automatic escaping for the output-similar to applying the `| raw` filter-unless you explicitly override it by providing the `is_safe` option in the registration options (e.g. `options: { is_safe: [] }`).
269
287
270
288
The next example registers three Twig filters and three functions.
0 commit comments