Skip to main content

Overview

The FormsServiceProvider registers all services, components, views, translations, commands, and Blade directives for the Javaabu Forms package. Namespace: Javaabu\Forms Extends: Illuminate\Support\ServiceProvider

Class Definition

class FormsServiceProvider extends ServiceProvider

Methods

boot

public function boot(): void
Bootstraps the application services including:
  • Publishing configuration, translations, and views
  • Loading translations and views
  • Registering Blade directives
  • Registering component namespace
Returns: void

Publishes

When running in console mode, the following assets can be published: Configuration:
php artisan vendor:publish --tag=forms-config
Publishes config/config.php to config/forms.php Translations:
php artisan vendor:publish --tag=forms-translations
Publishes language files to lang/vendor/forms Views:
php artisan vendor:publish --tag=forms-views
Publishes view files to resources/views/vendor/forms

Blade Directives

Registers two custom Blade directives: @model Directive:
@model($user)
    <!-- Form components here -->
@endmodel
Compiles to:
<?php app(\Javaabu\Forms\FormsDataBinder::class)->bind($user); ?>
@endmodel Directive:
@endmodel
Compiles to:
<?php app(\Javaabu\Forms\FormsDataBinder::class)->pop(); ?>

Component Namespace

Registers the component namespace forms for all components in Javaabu\Forms\Views\Components:
<x-forms::text name="email" />
<x-forms::submit>Save</x-forms::submit>

register

public function register(): void
Registers the application services including:
  • Merging configuration files
  • Registering the FormsDataBinder singleton
  • Registering the forms middleware alias
  • Registering Artisan commands
Returns: void

Services Registered

FormsDataBinder Singleton:
$this->app->singleton(FormsDataBinder::class, fn () => new FormsDataBinder());
Forms Middleware:
app('router')->aliasMiddleware('forms', OverrideFormsDefaults::class);

Commands Registered

When running in console mode:
  • Javaabu\Forms\Commands\PublishViewCommand
  • Javaabu\Forms\Commands\DiffViewsCommand

mergeConfig

protected function mergeConfig(): void
Merges package configuration with user-defined configuration. Performs deep merging for the nested frameworks configuration. Returns: void Configuration Merging:
  1. Merges top-level config:
    $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'forms');
    
  2. Deep merges each framework configuration:
    foreach ($default_frameworks as $framework => $configs) {
        $user_config = config('forms.frameworks.' . $framework);
        config()->set(
            'forms.frameworks.' . $framework,
            array_merge($configs, is_array($user_config) ? $user_config : [])
        );
    }
    
This ensures that user-defined framework configurations extend rather than replace the default configurations.

Usage Example

Registering the Service Provider

In Laravel 11+, the service provider is auto-discovered. For older versions, add to config/app.php:
'providers' => [
    // Other providers...
    Javaabu\Forms\FormsServiceProvider::class,
],

Publishing Assets

# Publish all assets
php artisan vendor:publish --provider="Javaabu\Forms\FormsServiceProvider"

# Publish specific assets
php artisan vendor:publish --tag=forms-config
php artisan vendor:publish --tag=forms-views
php artisan vendor:publish --tag=forms-translations

Using Registered Services

Access FormsDataBinder:
$binder = app(Javaabu\Forms\FormsDataBinder::class);
$binder->bind($user);
Use Forms Middleware:
Route::middleware('forms')->group(function () {
    // Routes that need form defaults override
});
Use Blade Directives:
@model($user)
    <x-forms::text name="name" />
@endmodel
Use Components:
<x-forms::form method="POST" action="/users">
    <x-forms::text name="email" />
    <x-forms::submit>Create</x-forms::submit>
</x-forms::form>

Configuration Structure

The service provider expects a configuration file with the following structure:
return [
    'framework' => 'bootstrap5',
    
    'frameworks' => [
        'bootstrap5' => [
            'icon-prefix' => 'fas fa',
            // Other bootstrap5 configs...
        ],
        'tailwind' => [
            'icon-prefix' => 'fa-solid',
            // Other tailwind configs...
        ],
    ],
    
    'use_eloquent_date_casting' => true,
    
    // Other configurations...
];

Registered Commands

The service provider registers two Artisan commands:
  1. forms:publish-view - Publish specific view files for customization
  2. forms:diff-views - Compare published views with package views
See the PublishViewCommand and DiffViewsCommand documentation for details.

Build docs developers (and LLMs) love