laravel 自定义门面及部分表单封装

在app下新建Form文件夹

新建类MyForm并封装部分常用表单

namespace App\Form;

class MyForm {
    /**
     * 普通输入框
     */
    public function text_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'pencil';
        $type = 'text';
        $this->input_field($icon, $type, $label, $name, $value, $module, $helper,  $required);
    }
     /**
     * 手机输入框
     */
    public function phone_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'mobile-phone';
        $type = 'text';
        $this->input_field($icon, $type, $label, $name, $value, $module, $helper,  $required);
    }
    /**
     * 上传图片
     */
    public function image_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'file-image-o';
        $type = 'file';
       
    }
    /**
     * 上传视频
     */
    public function video_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'file-video-o';
        $type = 'file';
        
    }
    /**
     * 多图上传
     */
    public function multiple_field($label, $name, $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'copy';
        $type = 'file';
        
    }
    /**
     * 下拉列表
     */
    public function select_list($label, $name, array $data,  $value = '', $module = '', $required = false, $helper = '') {
        $icon = 'align-justify';
        $id_value = $name;
        $name_value = $name;
        if (!empty($module)) {
            $id_value = $module . '_' . $name;
            $name_value = $module . '[' . $name .']';
        }
        $options = '';
        foreach($data as $key => $val) {
            $options .= '<option value="'. $key .'">'. $val .'</option>';
        }
        $field = '<select id="'. $id_value .'" name="' . $name_value . '" class="form-control">'. $options .'</select>';
        $this->form_group($label, $icon, $field, $helper, $required);
    }
    /**
     * 单选
     */
    public function radio_list($label, $name, array $data, $value = '', $module = '', $required = false, $helper = '') {
        $icon = '';
        $id_value = $name;
        $name_value = $name;
        if (!empty($module)) {
            $id_value = $module . '_' . $name;
            $name_value = $module . '[' . $name .']';
        }
        $field = '<div>';
        foreach($data as $key => $val) {
            $field .= '<label class="radio-inline"><input type="radio" id="'. $id_value . '_' . $key .'" name="'. $name_value .'" value="'. $key .'">'. $val . '</label>';
        }
        $field .= '</div>';
        $this->form_group($label, $icon, $field, $helper, $required);
    }


    private function input_field ($icon, $type, $label, $name, $value, $module, $helper, $required) {
        $id_value = $name;
        $name_value = $name;
        if (!empty($module)) {
            $id_value = $module . '_' . $name;
            $name_value = $module . '[' . $name .']';
        }
        $field = '<input type="'. $type .'" id="'. $id_value .'" name="'. $name_value .'" value="'. $value .'" class="form-control" placeholder="请输入'. $label .'">';
        $this->form_group($label, $icon, $field, $helper, $required);
    }

    private function form_group($label, $icon, $field, $helper, $required) {
        $form_start = '<div class="form-group">';
        if ($required) {
            $label = $label. ' <span>*</span>';
        }
        $form_label = '<label class="col-sm-2  control-label">'. $label .'</label>';
        $form_icon = '';
        if(!empty($icon)) {
            $form_icon = '<span class="input-group-addon"><i class="fa fa-'. $icon .' fa-fw"></i></span>';
        }
        $form_helper = '';
        if (!empty($helper)) {
            $form_helper = '<p class="help-block">'. $helper .'</p>';
        }
        $form_input = '<div class="col-sm-8"><div class="input-group">' .$form_icon . $field . '</div>' . $form_helper;
        $form_end = '</div></div>';

        $form = $form_start . $form_label . $form_input . $form_end;
        echo $form;
    }
}

 

 

自定义门面

新建表单门面MyFormFacade

 

namespace App\Form;

use Illuminate\Support\Facades\Facade;

class MyFormFacade extends Facade {
    protected static function getFacadeAccessor()
    {
        return 'MyForm';
    }
}

注册门面

在app/providers下新建表单服务FormServiceProvider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Form\MyForm;

class FormServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('MyForm',function(){
            //return new TestService();
            return new MyForm;
        });
    }
}

 

在app/config.php中注册

在providers下注册服务

'providers' => [
        App\Providers\FormServiceProvider::class,

    ],

在aliases下注册门面

'aliases' => [

        'MyFormFacade' => App\Form\MyFormFacade::class,

    ],

 

不要忘记 composer dump-autoload

使用

在resources/views中新建form.blade.php

{!! MyFormFacade::text_field('标题', 'title', '1', 'article', true) !!}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值