|
| 1 | +--- |
| 2 | +title: Overriding Classes |
| 3 | +sort: 3 |
| 4 | +--- |
| 5 | + |
| 6 | +## Overriding Classes |
| 7 | + |
| 8 | +When you override a component class with your own, you will need to update the class referenced for the component alias in the config. |
| 9 | +For the `select` component, it would look like this: |
| 10 | + |
| 11 | +**Custom Class:** |
| 12 | +```php |
| 13 | +<?php |
| 14 | + |
| 15 | +namespace App\View; |
| 16 | + |
| 17 | +use Rawilk\FormComponents\Components\Inputs\Select; |
| 18 | + |
| 19 | +class MySelect extends Select |
| 20 | +{ |
| 21 | + // Override stuff here... |
| 22 | + |
| 23 | + /* |
| 24 | + * You will need to override this method to |
| 25 | + * let the BladeComponent parent class |
| 26 | + * know where to look for this component's |
| 27 | + * view. |
| 28 | + * |
| 29 | + * Alternatively, you can override the |
| 30 | + * "render" method. |
| 31 | + */ |
| 32 | + public static function getName(): string |
| 33 | + { |
| 34 | + return 'inputs.select'; |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +**Config:** |
| 40 | +```php |
| 41 | +<?php |
| 42 | + |
| 43 | +return [ |
| 44 | + 'components' => [ |
| 45 | + // ... |
| 46 | + 'select' => \App\View\MySelect::class, |
| 47 | + ], |
| 48 | + |
| 49 | + // ... |
| 50 | +]; |
| 51 | +``` |
| 52 | + |
| 53 | +> {note} If you choose to override a class, you will need to override either the `getName` method (as shown in code above) |
| 54 | +> or override the `render` method, so the parent `BladeComponent` class will use the correct view. |
| 55 | +
|
| 56 | +Some component classes will also need to be bound in the container in a service provider since internally this package does not |
| 57 | +reference components by alias. One of those components is the `label` component, which is referenced by the `form-group` component |
| 58 | +internally. To have the correct class be used, you can bind the overridden class in a service provider in the `register` method: |
| 59 | + |
| 60 | +```php |
| 61 | +<?php |
| 62 | + |
| 63 | +namespace App\Providers; |
| 64 | + |
| 65 | +use App\View\MyCustomLabelClass; |
| 66 | +use Illuminate\Support\ServiceProvider; |
| 67 | +use Rawilk\FormComponents\Components\Label; |
| 68 | + |
| 69 | +class AppServiceProvider extends ServiceProvider |
| 70 | +{ |
| 71 | + public function register() |
| 72 | + { |
| 73 | + $this->app->bind(Label::class, MyCustomLabelClass::class); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Any component classes you override should follow the same pattern as illustrated above in a service provider. |
0 commit comments