Skip to content

Commit 0f24b23

Browse files
committed
wip
1 parent 8f295b5 commit 0f24b23

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Operations Lifecycle
2+
3+
--
4+
5+
<a name="about"></a>
6+
## About
7+
8+
At important points in the CRUD Lifecycle, Backpack triggers what we call "lifecycle events". You can hook into those events - by registering custom code that will run when that lifecycle event happens. This allows you to customize the process, without having to override any of the core files for CRUD or an Operation.
9+
10+
For example, in a Backpack CRUD all routes are setup on the **CrudController** using methods like `setupModerateOperationRoutes()`. Before those methods are called, Backpack calls `LifecycleEvent::trigger('crud:before_all_route_setup')`. If you want to add your own code that runs there, you can do:
11+
12+
```php
13+
LifecycleEvent::hookInto('crud:before_setup_routes', function($controller) {
14+
// do something before the routes are setup
15+
});
16+
```
17+
<a name="hooks-usage"></a>
18+
Here are all the general Lifecycle Events we currently have:
19+
20+
`crud:before_setup_routes` - before any operation routes are registered
21+
`crud:after_setup_routes` - after all operation routes have been registered
22+
`crud:before_setup_defaults` - before all defaults are setup
23+
`crud:after_setup_defaults` - after all defaults have been setup
24+
`crud:before_setup` - before any operation is set up
25+
`crud:after_setup` - after that operation has been set up
26+
27+
28+
In addition to the general Lifecycle events above, each operation can trigger its own lifecycle events. For example, here are the lifecycle events triggered by the Create operation:
29+
30+
`create:before_setup` - exposes parameters: $crud
31+
`create:after_setup` - exposes parameters: $crud
32+
33+
You can hook into those events using a similar syntax to the general lifecycle events:
34+
35+
```php
36+
LifecycleEvent::hookInto(['create:before_setup'], function() {
37+
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create');
38+
});
39+
```
40+
41+
Note that when using the hooks for specific operations, the hook is prefixed with the operation name followed by the hook name. This allow you to hook into specific operation events, or even to multiple events at the same time:
42+
43+
```php
44+
LifecycleEvent::hookInto(['create:before_setup', 'list:before_setup'], function() {
45+
// do something before the create operation and the list operation are setup
46+
});
47+
```
48+
49+
<a name="how-to-add-your-own-hooks"></a>
50+
51+
As a developer you may have had the need to create custom operations, and while creating a "one time use" operation may not require/demand the usage of lifecycle events, creating a reusable operation that you may want to share with the community, or use in multiple projects, may benefit from the usage of lifecycle events to allow other developers to hook into your operation and customize its behavior.
52+
53+
You can add your own lifecycle events to your custom operations by calling the `LifecycleEvent::trigger()` method at the appropriate points in your operation. For example, if you have a custom operation that need to do something after some action happen in te operation, you can trigger a lifecycle event like this:
54+
55+
```php
56+
public function moderate() {
57+
// do something to "moderate" the entry and register the hook
58+
LifecycleEvent::trigger('moderate:after_moderation', [
59+
'controller' => $this,
60+
'operation' => 'moderate',
61+
]);
62+
}
63+
```
64+
65+
Then, other developers can hook into that event like this:
66+
67+
```php
68+
LifecycleEvent::hookInto(['moderate:after_moderation'], function($controller, $operation) {
69+
// do something after the moderate operation has been executed
70+
});
71+
```
72+

7.x-dev/crud-operations.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ You'll notice the generated operation has:
261261

262262
You can customize these to fit the operation you have in mind, then ```use \App\Http\Controllers\Admin\Operations\CommentOperation;``` inside the CrudControllers where you want the operation.
263263

264+
<a name="operations-lifecycle-hooks"></a>
265+
### Operations Lifecycle Hooks
266+
267+
Backpack operations trigger lifecycle events at various points in their execution. You can hook into these events to customize the operation's behavior. You can read a detailed guide about [how to use lifecycle hooks](/docs/{{version}}/crud-operations-lifecycle).
268+
264269
<a name="contents-of-a-custom-operation"></a>
265270
### Contents of a Custom Operation
266271

7.x-dev/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
+ [Delete](/docs/{{version}}/crud-operation-delete)
4545
+ [Show](/docs/{{version}}/crud-operation-show)
4646
+ [Columns](/docs/{{version}}/crud-columns)
47+
+ [Operations Lifecycle Hooks](/docs/{{version}}/crud-operations-lifecycle)
4748
- [Additional Operations](/docs/{{version}}/crud-operations)
4849
+ [Clone](/docs/{{version}}/crud-operation-clone)
4950
+ [Reorder](/docs/{{version}}/crud-operation-reorder)

0 commit comments

Comments
 (0)