php – Laravel扩展Form类

前端之家收集整理的这篇文章主要介绍了php – Laravel扩展Form类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试扩展L4.1中的Form类,但我似乎错过了一些东西.我的文件基于API命名为FormBuilder.PHP,保存在app / libraries / extended / FormBuilder.PHP中.
<?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);
}

}

这实际上是我第一次尝试在Laravel中扩展核心类.我似乎无法指出如何正确扩展像这个Form类的核心类.

编辑:
我将“app / libraries / extended”添加到我的composer.json文件中并运行了composer.phar update和composer.phar dump-autoload,但它似乎仍然使用核心类而不是扩展类.我忘了做什么?

要扩展/交换Laravel核心类,您可以创建服务提供者:

文件: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');
    }

}

为它创建一个Facade:

文件: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'; }

}

这将是您的命名空间服务类:

文件: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,$options = array())
    {
        $options = $options + array('id'=>"field-{$name}");

        return $this->input('text',$options);
    }

}

将app / config / app.PHP和您的服务提供商打开到列表中

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

并用你的替换Laravel的Form别名

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

为了测试,我创建了一个这样的路由器:

Route::any('test',function() {

   return e(Form::text('first_name'));

});

它给了我这个结果:

<input id="field-first_name" name="first_name" type="text">
原文链接:https://www.f2er.com/laravel/133280.html

猜你在找的Laravel相关文章