novius/laravel-backpack-visualcomposer

此包提供了一个易于管理页面内容的界面

2.0.0 2019-01-31 11:49 UTC

This package is auto-updated.

Last update: 2024-09-26 11:20:21 UTC


README

Travis Packagist Release Licence

改进编辑页面的方式。

安装

composer require novius/laravel-backpack-visualcomposer

然后将其添加到 config/app.php

Novius\Backpack\VisualComposer\VisualComposerServiceProvider::class,

最后,运行

php artisan migrate

使用

在模型中

use \Novius\Backpack\VisualComposer\Traits\VisualComposer;

在CRUD控制器中

public function setup($template_name = false)
{
    parent::setup($template_name);

    $this->crud->addField([
        'name' => 'visualcomposer_main',
        'label' => 'Visual Composer',
        'type' => 'view',
        'view'   => 'visualcomposer::visualcomposer',
        // (optionnal) Only those template will be available
        'templates' => [
            MyNewRowTemplate::class,
        ],
        // (optionnal) Pre-fill the visualcomposer with rows on new models
        'default' => [
            ['template' => MyNewRowTemplate::class],
        ],
        'wrapperAttributes' => [
            'class' => 'form-group col-md-12',
        ],
    ]);
}

public function store(PageRequest $request)
{
    $r = parent::store($request);
    $this->crud->entry->visualcomposer_main = $request->visualcomposer_main;
    return $r;
}

public function update(PageRequest $request)
{
    $r = parent::update($request);
    $this->crud->entry->visualcomposer_main = $request->visualcomposer_main;
    return $r;
}

在模型视图中

@foreach($page->visualcomposer_main as $row)
    {!! $row->template::renderFront($row) !!}
@endforeach

创建新的行模板

为视图创建一个类和文件夹

cd vendor/novius/laravel-backpack-visualcomposer
class=MyNewRowTemplate
touch src/app/Templates/$class.php
mkdir src/resources/views/vendor/visualcomposer/$class
touch src/resources/views/vendor/visualcomposer/$class/crud.blade.php
touch src/resources/views/vendor/visualcomposer/$class/front.blade.php

MyNewRowTemplate.php

<?php

namespace Novius\Backpack\VisualComposer\Templates;

class MyNewRowTemplate extends RowTemplateAbstract
{
    static public $name = 'my-new-row-template';
}

crud.blade.php

<div class="row-template vc-my-new-row-template">
    <input type="hidden" class="content">
    <textarea class="some_field"
              placeholder="{{ trans('visualcomposer::my-new-row-template.crud.some_field') }}"></textarea>
</div>

@push('crud_fields_scripts')
    <script>
        window['vc_boot', {!!json_encode($template)!!}] = function ($row, content)
        {
            var $hiddenInput = $(".content[type=hidden]", $row);
            var fields = [
                'some_field',
            ];

            // Setup update routine
            var update = function () {
                var contents = [];
                fields.map(function (item) {
                    contents.push($('.'+item, $row).val());
                });
                $hiddenInput.val(
                    JSON.stringify(contents)
                );
            };

            // Parse and fill fields from json passed in params
            fields.map(function (item, index) {
                try {
                    $('.'+item, $row).val(JSON.parse(content)[index]);
                } catch(e) {
                    console.log('Empty or invalid json:', content);
                }
            });

            // Update hidden field on change
            $row.on(
                'change blur keyup',
                'input, textarea, select',
                update
            );

            // Initialize hidden form input in case we submit with no change
            update();
        }
    </script>
@endpush

resources/lang/vendor/visualcomposer/en/templates.php 中添加

<?php
...
    'my-new-row-template' => [
        'name' => 'My new row template',
        'crud' => [
            'some_field' => 'Some field for you to write something in',
        ],
    ],

编辑默认配置和模板

运行

php artisan vendor:publish --provider="Novius\Backpack\VisualComposer\VisualComposerServiceProvider"

...它将输出可以现在覆盖的文件列表,包括配置、backpack字段类型、语言文件和11个内置模板

  • 文章,一个 wysiwyg 和标题、副标题、日期、作者、CTA 按钮以及用户可定制的颜色输入
  • 背景图片和文字,一个可上传的图片、标题和 wysiwyg 描述
  • Base64图片,一个以 base64 存储的图片而不是文件上传
  • 容器中的图片,一个可上传的图片,仅此而已
  • 左图右文,两个栏中的图片和一些文字字段
  • 右图左文,两个栏中的文字字段和图片
  • 左文右引用,两个栏中的文字字段和可定制的背景颜色
  • 最小化,一个空模板,具有使它工作的最小代码
  • 幻灯片,无限图片和它们的标题的滑块
  • 三栏图片文字CTA,每列都有一个图片、标题、wysiwyg 和 CTA 按钮
  • 两栏图片文字CTA,与上述相同,但只有两列而不是三列

查看它们的制作方式,以便您可以自定义它们并构建自己的。