|
| 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 | + |
0 commit comments