rainlab/livewire-plugin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rainlab/livewire-plugin

Composer 安装命令:

composer require rainlab/livewire-plugin

包简介

Load and render Livewire components using Twig inside October CMS themes.

README 文档

README

Integrate Laravel Livewire components inside October CMS themes, app and plugins, with templates provided by Twig, Blade or PHP.

Requirements

  • October CMS v3.3.9 or above

Installation

To install with Composer, run this from your project root.

composer require rainlab/livewire-plugin

Rendering Livewire Components

Use the livewire CMS component to activate Livewire in your CMS theme page or layout. For example, your page may look like this.

url = "/test"
layout = "default"

[livewire]
==
<div class="container">
    {% livewire 'counter' %}
</div>

Next, render a Livewire component using the {% livewire %} Twig tag.

{% livewire 'counter' %}

You may pass variables to the component using an equal sign (=).

{% livewire 'counter' count=3 %}

Note: For proper operation, your CMS layout should include the {% styles %} and {% scripts %} placeholder tags, as described in the placeholder documentation.

File Locations

By default, classes are stored in the app/Livewire directory. However, you can register custom classes within plugins (see below).

Views are stored in the in app/views/livewire directory by default. The following template syntaxes are supported in the views directory, determined by their file extension.

Extension Template Engine
.htm October Twig
.blade.php Laravel Blade
.php PHP Template

Plugin Registration

To register Livewire components in your plugins using the registerLivewireComponents override method. The method should return a class name as the key and the component alias as the value.

public function registerLivewireComponents()
{
    return [
        \October\Demo\Livewire\Todo::class => 'demoTodo'
    ];
}

In the above example, the October\Demo\Livewire\Todo class refers to the following file locations:

  • Class file: plugins/october/demo/livewire/Todo.php
  • View file: plugins/october/demo/views/livewire/todo.htm.

The class should return its view path by overriding the render method, and returns a View instance relative to the plugin.

namespace October\Demo\Livewire;

use RainLab\Livewire\Classes\LivewireBase;

class Todo extends LivewireBase
{
    public function render()
    {
        return \View::make('october.demo::livewire.todo');
    }
}

The component can be rendered anywhere using the demoTodo alias.

{% livewire 'demoTodo' %}

Usage in CMS Components

You may implement Livewire in your CMS components using the RainLab\Livewire\Behaviors\LivewireComponent behavior. This implementation will ensure that the necessary dependencies are registered when the component is used.

class MyComponent extends \Cms\Classes\ComponentBase
{
    public $implement = [
        \RainLab\Livewire\Behaviors\LivewireComponent::class
    ];
}

Then render the component using the {% livewire %} tag.

{% livewire 'counter' %}

Alternatively, you can render the component in PHP using the renderLivewire method.

$this->renderLivewire('counter');

Usage in Backend Controllers

You may implement Livewire in your backend controllers using the RainLab\Livewire\Behaviors\LivewireController behavior. This implementation will ensure that the necessary dependencies are registered with the controller.

class MyController extends \Backend\Classes\Controller
{
    public $implement = [
        \RainLab\Livewire\Behaviors\LivewireController::class
    ];
}

Then render the component using the makeLivewire method.

<?= $this->makeLivewire('counter') ?>

Usage Example

Here we will create a component in the app directory and render it on a CMS page freestanding. First, create a file called app/views/livewire/counter.htm with the following content.

<div class="input-group py-3 w-25">
    <button wire:click="add" class="btn btn-outline-secondary">
        Add
    </button>
    <div class="form-control">
        {{ count }}
    </div>
    <button wire:click="subtract" class="btn btn-outline-secondary">
        Subtract
    </button>
</div>

Create a file called app/Livewire/Counter.php with the following contents.

<?php namespace App\Livewire;

use RainLab\Livewire\Classes\LivewireBase;

class Counter extends LivewireBase
{
    public $count = 1;

    public function add()
    {
        $this->count++;
    }

    public function subtract()
    {
        $this->count--;
    }
}

This component now be rendered as counter in your CMS templates. The component name is derived from the class name.

{% livewire 'counter' %}

For example, in the demo theme, create a template called test.htm with the following content. Then open the /test URL.

url = "/test"
layout = "default"

[livewire]
==
<div class="container">
    {% livewire 'counter' %}
</div>

See Also

License

This plugin is an official extension of the October CMS platform and is free to use if you have a platform license. See EULA license for more details.

rainlab/livewire-plugin 适用场景与选型建议

rainlab/livewire-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 522 次下载、GitHub Stars 达 28, 最近一次更新时间为 2023 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rainlab/livewire-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rainlab/livewire-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 rainlab/livewire-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 522
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 7
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-03-29