Laravel extend Form class

后端 未结 1 431
情话喂你
情话喂你 2020-12-03 09:14

I\'m trying to extend the Form class in L4.1 but I seem to be missing something. My file is named FormBuilder.php based on the API and is saved in

相关标签:
1条回答
  • 2020-12-03 09:43

    To extend/swap a Laravel core class, you can create a Service Provider:

    File: app/App/Libraries/Extensions/FormBuilder/FormBuilderServiceProvider.php

    <?php namespace App\Libraries\Extensions\FormBuilder;
    
    use Illuminate\Support\ServiceProvider as  IlluminateServiceProvider;
    use App\Libraries\Extensions\FormBuilder\FormBuilder;
    
    class FormBuilderServiceProvider extends IlluminateServiceProvider {
    
        /**
         * Indicates if loading of the provider is deferred.
         *
         * @var bool
         */
        protected $defer = true;
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            $this->app->bindShared('formbuilder', function($app)
            {
                $form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
    
                return $form->setSessionStore($app['session.store']);
            });
        }
    
        /**
         * Get the services provided by the provider.
         *
         * @return array
         */
        public function provides()
        {
            return array('formbuilder');
        }
    
    }
    

    Create a Facade for it:

    File: app/App/Libraries/Extensions/FormBuilder/FormBuilderFacade.php

    <?php namespace App\Libraries\Extensions\FormBuilder;
    
    use Illuminate\Support\Facades\Facade as IlluminateFacade;
    
    class FormBuilderFacade extends IlluminateFacade {
    
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() { return 'formbuilder'; }
    
    }
    

    This would be your namespaced service class:

    File: app/App/Libraries/Extensions/FormBuilder/FormBuilder.php

    <?php namespace App\Libraries\Extensions\FormBuilder;
    
    use \Illuminate\Html\FormBuilder as IlluminateFormBuilder;
    
    class FormBuilder extends IlluminateFormBuilder {
    
        public function text($name, $value = null, $options = array())
        {
            $options = $options + array('id'=>"field-{$name}");
    
            return $this->input('text', $name, $value, $options);
        }
    
    }
    

    Open app/config/app.php and your Service Provider to the list

    'App\Libraries\Extensions\FormBuilder\FormBuilderServiceProvider',
    

    And replace Laravel's Form alias with yours

        'Form'            => 'App\Libraries\Extensions\FormBuilder\FormBuilderFacade',
    

    To test I created a router like this:

    Route::any('test', function() {
    
       return e(Form::text('first_name'));
    
    });
    

    And it gave me this result:

    <input id="field-first_name" name="first_name" type="text">
    
    0 讨论(0)
提交回复
热议问题