>本文深入研究構建一個Octincms後端窗口小部件插件,並在基本插件創建上擴展。我們將開發一個“快速註釋”窗口小部件,鏡像WordPress的快速草稿功能。
>
密鑰概念:
php artisan create:plugin
>。 QuickNoteWidget
)。
defineProperties
> >我們將創建一個小部件,允許用戶直接從其Octcms儀表板中快速添加和管理簡短的筆記。
>
使用命令行生成插件:
> 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.php
在widgets
中註冊小部件:
<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.htm
widgets/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。控制器():
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
5。 後端列表和表單配置:
>如原始響應中所示,使用config_list.yaml
和config_form.yaml
配置後端列表和表單。 請密切注意控制器中的listExtendQueryBefore
方法,以確保僅顯示當前用戶的筆記。 表單配置的屏幕截圖很有用:
>此詳細的故障應幫助您構建快速註釋小部件。請記住,請諮詢“十月”文檔以獲取進一步的幫助,並根據您的特定需求調整代碼。 原始響應結束時的常見問題解答為與十月cms合作提供了其他有用的信息。
>以上是如何構建一個十月cms小部件插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!