Skip to content

Commit 02b74d4

Browse files
committed
polish docs for lifecycle hooks
1 parent adf9de1 commit 02b74d4

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

7.x-dev/crud-operations.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,31 @@ Understanding these moments and their order, is important in order to place your
226226
<a name="lifecycle-hooks"></a>
227227
### Lifecycle Hooks
228228

229-
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.
229+
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 that CRUD or Operation.
230230

231-
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:
231+
For example, in a Backpack CRUD all routes are setup on the **CrudController** using methods like `setupModerateOperationRoutes()`. Before those methods are called, Backpack triggers an event called `crud:before_all_route_setup`. If you want to add your own code that runs there, you can do:
232232

233233
```php
234+
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
235+
234236
LifecycleEvent::hookInto('crud:before_setup_routes', function($controller) {
235237
// do something before the routes are setup
236238
});
237239
```
238-
<a name="hooks-usage"></a>
239-
Here are all the general Lifecycle Events we currently have:
240+
<a name="general-lifecycle-events"></a>
241+
#### General Lifecycle Events
242+
243+
Here are all the general lifecycle events Backpack triggers:
240244

241-
`crud:before_setup_routes` - before any operation routes are registered
242-
`crud:after_setup_routes` - after all operation routes have been registered
243-
`crud:before_setup_defaults` - before all defaults are setup
244-
`crud:after_setup_defaults` - after all defaults have been setup
245-
`crud:before_setup` - before any operation is set up
246-
`crud:after_setup` - after that operation has been set up
245+
- `crud:before_setup_routes` - before any operation routes are registered
246+
- `crud:after_setup_routes` - after all operation routes have been registered
247+
- `crud:before_setup_defaults` - before all defaults are setup
248+
- `crud:after_setup_defaults` - after all defaults have been setup
249+
- `crud:before_setup` - before any operation is set up
250+
- `crud:after_setup` - after that operation has been set up
247251

252+
<a name="operation-lifecycle-events"></a>
253+
#### Operation Lifecycle Events
248254

249255
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:
250256

@@ -268,10 +274,9 @@ LifecycleEvent::hookInto(['create:before_setup', 'list:before_setup'], function(
268274
```
269275

270276
<a name="how-to-add-your-own-hooks"></a>
277+
### How to add your own hooks
271278

272-
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.
273-
274-
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:
279+
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 happens in the operation, you can trigger a lifecycle event like this:
275280

276281
```php
277282
public function moderate() {

7.x-dev/release-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Together, our team has put in an incredible amount of work to make v7 what it is
2222

2323
### CRUD Lifecycle Hooks
2424

25-
// TODO
25+
Previously when working with Operations, developers found themselves needing to _override_ an entire operation method, in order to do things before/after the routes/defaults/operation is set up. This created a lot of duplicate code, and made it hard to maintain. Now, you can use CRUD Lifecycle Hooks to add your own code before/after each operation method. This is a much cleaner way to add your own code, without having to override the entire method. [Read more](/docs/{{version}}/crud-operations#lifecycle-hooks).
2626

2727
### Two-Factor Authentication
2828

0 commit comments

Comments
 (0)