Laravel扩展表单辅助类


Laravel Extend Form Helper Class

我使用的是Laravel 5.1,并安装了Collective的HTML和FORM助手。

我现在试图扩展这个来处理几个自定义表单元素,但我努力让它工作。我当前的错误是:

 Argument 1 passed to Collective'Html'FormBuilder::__construct() must be an instance of Collective'Html'HtmlBuilder, instance of Illuminate'Foundation'Application given

我的类扩展FormBuilder如下:

namespace Helpers;

使用' ' Html ' FormBuilder集体;

class MyFormHelper extends FormBuilder
{

    public function arrayHidden($name, $value = null, $options = array())
    {
        ddd($value);
        return $this->input('hidden', $name, $value, $options);
    }
}

所以现在我只想求出$value

我对扩展类的知识有限,但我认为这是可行的

包中的FormBuilder类是:
<?php namespace Collective'Html;
use DateTime;
use Illuminate'Routing'UrlGenerator;
use Illuminate'Session'Store as Session;
use Illuminate'Support'Traits'Macroable;
class FormBuilder {
use Macroable;
/**
 * The HTML builder instance.
 *
 * @var 'Collective'Html'HtmlBuilder
 */
protected $html;
/**
 * The URL generator instance.
 *
 * @var 'Illuminate'Routing'UrlGenerator  $url
 */
protected $url;
/**
 * The CSRF token used by the form builder.
 *
 * @var string
 */
protected $csrfToken;
/**
 * The session store implementation.
 *
 * @var 'Illuminate'Session'Store
 */
protected $session;
/**
 * The current model instance for the form.
 *
 * @var mixed
 */
protected $model;
/**
 * An array of label names we've created.
 *
 * @var array
 */
protected $labels = array();
/**
 * The reserved form open attributes.
 *
 * @var array
 */
protected $reserved = array('method', 'url', 'route', 'action', 'files');
/**
 * The form methods that should be spoofed, in uppercase.
 *
 * @var array
 */
protected $spoofedMethods = array('DELETE', 'PATCH', 'PUT');
/**
 * The types of inputs to not fill values on by default.
 *
 * @var array
 */
protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');
/**
 * Create a new form builder instance.
 *
 * @param  'Illuminate'Routing'UrlGenerator  $url
 * @param  'Collective'Html'HtmlBuilder  $html
 * @param  string  $csrfToken
 * @return void
 */
public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken)
{
    $this->url = $url;
    $this->html = $html;
    $this->csrfToken = $csrfToken;
}

所以-我如何正确地扩展FormBuilder添加自定义函数?

谢谢

所以要扩展FormBuilder有两个步骤,你必须采取。你已经完成了第一个,那就是创建你自己的类,从laravel物理上扩展现有的FormBuilder类。

必须采取的第二步是在laravel的容器中注册这个新类。要做到这一点,你可以像下面这样设置一个新的服务提供者。

class FormBuilderServiceProvider extends Illuminate'Support'ServiceProvider {
    // Wait until all other service providers have been run before ours
    protected $defer = true;
    public function register()
    {
        $this->app->bindShared('formbuilder', function($app)
        {
            $form = new 'Your'FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
            return $form->setSessionStore($app['session.store']);
        });
    }
    public function provides()
    {
        return ['formbuilder'];
    }
}

您现在只需要通过删除现有的服务提供商并用新的服务提供商替换它来更新您的config/app.php

你可以在laravel文档的服务容器和服务提供者页面中阅读更多信息。

http://laravel.com/docs/5.1/containerhttp://laravel.com/docs/5.1/providers