Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

php - Laravel extend Form class

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 app/libraries/extended/FormBuilder.php.

<?php namespace Extended;

class FormBuilder extends IlluminateHtmlFormBuilder {

/**
 * 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 actually the first time I've tried extending a core class in Laravel. I can't seem to put my finger on how to properly extend core 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 be using the core class instead of my extended one. What am I forgetting to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

<?php namespace AppLibrariesExtensionsFormBuilder;

use IlluminateSupportServiceProvider as  IlluminateServiceProvider;
use AppLibrariesExtensionsFormBuilderFormBuilder;

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 AppLibrariesExtensionsFormBuilder;

use IlluminateSupportFacadesFacade 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 AppLibrariesExtensionsFormBuilder;

use IlluminateHtmlFormBuilder 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

'AppLibrariesExtensionsFormBuilderFormBuilderServiceProvider',

And replace Laravel's Form alias with yours

    'Form'            => 'AppLibrariesExtensionsFormBuilderFormBuilderFacade',

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">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...