Skip to content

Commit abe1ae3

Browse files
authored
Clarify register() and boot() usage in Plugin registration file (#228) (#237)
Co-authored-by: Luke Towers <github@luketowers.ca>. Credit to @EmiFunes91. Closes #228
1 parent 4ecb06b commit abe1ae3

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

plugin/registration.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ The default author code offered by the Marketplace consists of the author first
128128

129129
## Registration file
130130

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:
132132

133133
1. Information about the plugin, its name, and author.
134134
1. Registration methods for extending the CMS and stating the intentions of the plugin.
@@ -199,24 +199,42 @@ Key | Description
199199

200200
## Routing and initialization
201201

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.
203203

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:**
205213

206214
```php
207215
public function register()
208216
{
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();
211219
});
212220
}
213221
```
214222

215223
> **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.
216224
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:**
218234

219235
```php
236+
use Backend\Models\User;
237+
220238
public function boot()
221239
{
222240
User::extend(function ($model) {
@@ -225,7 +243,7 @@ public function boot()
225243
}
226244
```
227245

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.
229247
230248
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:
231249

@@ -265,7 +283,7 @@ Custom Twig filters and functions can be registered in the CMS with the `registe
265283

266284
[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.
267285

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: [] }`).
269287
270288
The next example registers three Twig filters and three functions.
271289

0 commit comments

Comments
 (0)