Laravel扩展表助手类

时间:2015-07-23 08:17:58

标签: php laravel

我使用的是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;

使用\ Collective \ 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以添加自定义函数?

感谢

1 个答案:

答案 0 :(得分:3)

因此,要扩展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文档的Service Container和Service Providers页面上阅读更多相关内容。

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