首頁 >後端開發 >php教程 >如何構建一個十月cms小部件插件

如何構建一個十月cms小部件插件

Christopher Nolan
Christopher Nolan原創
2025-02-18 12:47:10178瀏覽

>本文深入研究構建一個Octincms後端窗口小部件插件,並在基本插件創建上擴展。我們將開發一個“快速註釋”窗口小部件,鏡像WordPress的快速草稿功能。

>

How to Build an OctoberCMS Widget Plugin

密鑰概念:

  • 插件用php artisan create:plugin>。
  • 擴展用戶模型以管理特定於用戶的註釋。
  • 為後端儀表板實現報告窗口小部件(QuickNoteWidget)。
  • >定義自定義的小部件屬性(
  • )(例如,隱藏註釋列表,更改標題)。 defineProperties
  • >利用控制器和模型進行形式處理,註釋管理(CRUD操作)。
  • >
構建快速註釋窗口小部件:

> >我們將創建一個小部件,允許用戶直接從其Octcms儀表板中快速添加和管理簡短的筆記。

>

How to Build an OctoberCMS Widget Plugin How to Build an OctoberCMS Widget Plugin

1。插件設置:

使用命令行生成插件:

> Update
<code class="language-bash">php artisan create:plugin RAFIE.quicknote</code>
帶有插件詳細信息:

Plugin.php

<code class="language-php">public function pluginDetails()
{
    return [
        'name'        => 'Quick Note Widget',
        'description' => 'Quickly add and manage short notes.',
        'author'      => 'RAFIE Younes',
        'icon'        => 'icon-pencil'
    ];
}</code>

version.yaml

<code class="language-yaml"># uploads/version.yaml
1.0.1: First version of quicknote
1.0.2:
  - Created Notes Table
  - create_notes_table.php</code>
2。數據庫模型:

創建

模型和遷移:

Note

遷移(
<code class="language-bash">php artisan create:model RAFIE.quicknote Note</code>
)將看起來像這樣:

> create_notes_table.php

>刷新插件:
<code class="language-php">Schema::create('rafie_quicknote_notes', function ($table) {
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->string('title', 255);
    $table->text('description')->nullable();
    $table->integer('user_id')->unsigned()->index();
    $table->timestamps();
});</code>

<code class="language-bash">php artisan plugin:refresh RAFIE.quicknote</code>
中擴展

模型:User> Plugin.php

<code class="language-php">public function boot()
{
    User::extend(function ($model) {
        $model->hasMany['notes'] = ['RAFIE\Quicknote\Models\Note'];
    });
}</code>
模型(

):Note> models/Note.php

<code class="language-php">use October\Rain\Database\Traits\Validation;

class Note extends Model
{
    use Validation;

    public $table = 'rafie_quicknote_notes';

    protected $guarded = ['*'];

    protected $rules = [
        'title' => 'required|min:4'
    ];

    public $belongsTo = ['user' => ['Backend\Models\User']];
}</code>
3。小部件創建:

create

(在文件夾中):>

QuickNoteWidget.phpwidgets中註冊小部件:

<code class="language-php">class QuickNoteWidget extends ReportWidgetBase
{
    public function render()
    {
        $notes = BackendAuth::getUser()->notes;
        return $this->makePartial('notes', ['notes' => $notes]);
    }

    public function defineProperties()
    {
        return [
            'title' => [
                'title' => 'Widget Title',
                'default' => 'Quick Note'
            ],
            'showList' => [
                'title' => 'Show Notes List',
                'type' => 'checkbox'
            ]
        ];
    }
}</code>

創建部分Plugin.php(在

中):
<code class="language-php">public function registerReportWidgets()
{
    return [
        'RAFIE\Quicknote\QuickNoteWidget' => [
            'label' => 'Quick Notes',
            'context' => 'dashboard'
        ]
    ];
}</code>
>

_notes.htmwidgets/quicknotewidget/partials

<code class="language-html"><div class="report-widget">
    <h3>{{ $this->property('title') }}</h3>
    <div class="pane">
        @if ($this->property('showList'))
            <ul class="list-nostyle">
                @foreach ($notes as $note)
                    <li class="list-group-item">{{ $note->title }}</li>
                @endforeach
            </ul>
        @endif
        <br>
        {{ Form::open(['url' => Backend::url('rafie/quicknote/notes/store'), 'method' => 'POST']) }}
        <div class="form-group">
            <input class="form-control" type="text" name="title" placeholder="Title" required>
        </div>
        <div class="form-group">
            <textarea class="form-control" name="description" id="" cols="30" rows="10" placeholder="Your note..."></textarea>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary" value="Submit">Submit</button>
            <a href="https://www.php.cn/link/b4c174fbc208372a8facfe462868ebf1'rafie/quicknote/notes/index')%20%7D%7D">Manage your notes</a>
        </div>
        {{ Form::close() }}
    </div>
</div></code>

4。控制器(How to Build an OctoberCMS Widget Plugin ):

controllers/Notes.php記住要完成剩餘的控制器操作(索引,創建,更新,刪除)及其關聯的配置文件(

<code class="language-php">class Notes extends Controller
{
    public function store()
    {
        $note = new Models\Note;
        $note->title = Input::get('title');
        $note->description = Input::get('description', null);
        $note->user_id = BackendAuth::getUser()->id;

        if ($note->save()) {
            Flash::success('Note added successfully.');
        } else {
            Flash::error('Validation error');
        }

        return Redirect::to(Backend::url());
    }

    // ... (rest of the controller code as described in the original response) ...
}</code>

,partials) ,如原始響應中所述。 顯示控制器結構的圖像很有幫助:config_list.yaml

How to Build an OctoberCMS Widget Plugin

5。 後端列表和表單配置:

>如原始響應中所示,使用config_list.yamlconfig_form.yaml配置後端列表和表單。 請密切注意控制器中的listExtendQueryBefore方法,以確保僅顯示當前用戶的筆記。 表單配置的屏幕截圖很有用:

How to Build an OctoberCMS Widget Plugin

>此詳細的故障應幫助您構建快速註釋小部件。請記住,請諮詢“十月”文檔以獲取進一步的幫助,並根據您的特定需求調整代碼。 原始響應結束時的常見問題解答為與十月cms合作提供了其他有用的信息。

>

以上是如何構建一個十月cms小部件插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn