Laravel 扩展 Form 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22884764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:18:50  来源:igfitidea点击:

Laravel extend Form class

phplaravellaravel-4extend

提问by enchance

I'm trying to extend the Formclass in L4.1 but I seem to be missing something. My file is named FormBuilder.phpbased on the API and is saved in app/libraries/extended/FormBuilder.php.

我正在尝试扩展FormL4.1 中的课程,但我似乎遗漏了一些东西。我的文件是FormBuilder.php基于 API命名的,并保存在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);
}

}

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 Formclass.

这实际上是我第一次尝试在 Laravel 中扩展核心类。我似乎无法确定如何正确扩展此类核心Form类。

Edit:I added "app/libraries/extended"to my composer.jsonfile and ran both composer.phar updateand composer.phar dump-autoloadbut it still seemed to be using the core class instead of my extended one. What am I forgetting to do?

编辑:我添加"app/libraries/extended"到我的composer.json文件并运行两个composer.phar updatecomposer.phar dump-autoload但它似乎仍然使用核心类而不是我的扩展类。我忘了做什么?

回答by Antonio Carlos Ribeiro

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

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

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

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

}

Create a Facade for it:

为它创建一个外观:

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

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

}

This would be your namespaced service class:

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

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

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

}

Open app/config/app.phpand your Service Provider to the list

打开app/config/app.php和您的服务提供商列表

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

And replace Laravel's Form alias with yours

并用你的替换 Laravel 的 Form 别名

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

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