Laravel 扩展表单类 [英] Laravel extend Form class

查看:19
本文介绍了Laravel 扩展表单类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展 L4.1 中的 Form 类,但我似乎遗漏了一些东西.我的文件基于 API 命名为 FormBuilder.php 并保存在 app/libraries/extended/FormBuilder.php 中.

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

<?php namespace Extended;

class FormBuilder extends IlluminateHtmlFormBuilder {

/**
 * 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 类这样的核心类.

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 Form class.

我将 "app/libraries/extended" 添加到我的 composer.json 文件中并同时运行了 composer.phar updatecomposer.phardump-autoload 但它似乎仍然使用核心类而不是我的扩展类.我忘了做什么?

I added "app/libraries/extended" to my composer.json file and ran both composer.phar update and composer.phar dump-autoload but it still seemed to be using the core class instead of my extended one. What am I forgetting to do?

推荐答案

要扩展/交换一个 Laravel 核心类,你可以创建一个 Service Provider:

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

文件:app/App/Libraries/Extensions/FormBuilder/FormBuilderServiceProvider.php

<?php namespace AppLibrariesExtensionsFormBuilder;

use IlluminateSupportServiceProvider as  IlluminateServiceProvider;
use AppLibrariesExtensionsFormBuilderFormBuilder;

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

}

为它创建一个外观:

文件:app/App/Libraries/Extensions/FormBuilder/FormBuilderFacade.php

<?php namespace AppLibrariesExtensionsFormBuilder;

use IlluminateSupportFacadesFacade 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:

文件:app/App/Libraries/Extensions/FormBuilder/FormBuilder.php

<?php namespace AppLibrariesExtensionsFormBuilder;

use IlluminateHtmlFormBuilder 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 和你的 Service Provider 到列表

Open app/config/app.php and your Service Provider to the list

'AppLibrariesExtensionsFormBuilderFormBuilderServiceProvider',

并用你的替换 Laravel 的表单别名

And replace Laravel's Form alias with yours

    'Form'            => 'AppLibrariesExtensionsFormBuilderFormBuilderFacade',

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

To test I created a router like this:

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

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

});

它给了我这个结果:

<input id="field-first_name" name="first_name" type="text">

这篇关于Laravel 扩展表单类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆