Laravel Form class extension

I am trying to extend the Form class in L4.1, but something seems to be missing. My file is called FormBuilder.php -based FormBuilder.php and is stored in the /library/extended/FormBuilder.php application .

 <?php namespace Extended; class FormBuilder extends \Illuminate\Html\FormBuilder { /** * Create a text input field. * * @param string $name * @param string $value * @param array $options * @return string */ public function text($name, $value = null, $options = array()) { $options = $options + array('id'=>"field-{$name}"); return $this->input('text', $name, $value, $options); } } 

This is the first time I have tried to extend the main class in Laravel. I can’t say how to properly extend the main classes like this Form class.

Edit: I added "app/libraries/extended" to my composer.json file and ran both composer.phar update and composer.phar dump-autoload , but it still seemed to use the main class instead of my extended one. What do I forget to do?

+6
source share
1 answer

To extend / replace the 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 will be your class of service with names:

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 in the list

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

And replace the Laravel form alias with yours

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

For testing, 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"> 
+12
source

All Articles