Laravel扩展了Form类

时间:2014-04-05 18:11:53

标签: php laravel laravel-4 extend

我正在尝试扩展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 updatecomposer.phar dump-autoload,但它似乎仍在使用核心类而不是扩展类。我忘记做什么了?

1 个答案:

答案 0 :(得分:13)

要扩展/交换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, $value = null, $options = array())
    {
        $options = $options + array('id'=>"field-{$name}");

        return $this->input('text', $name, $value, $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">