OctureCMS:プラグインの拡張性とプラグインの実用的なソフトウェアの削除の詳細な調査
開発者は一般に、使いやすくスケーラブルなCMSを好みます。 OctureCmsは、最初にシンプルさの概念を順守し、開発者とユーザーに心地よい体験をもたらします。この記事では、10月の拡張機能のいくつかを示し、単純なプラグインを使用した別のプラグインの機能を拡張します。
deleted_at
deleted_at
イベントに接続し、レコードの削除を妨げることによって行われます。代わりに、deleting
フィールドは現在のタイムスタンプに更新され、レコードは保存されます。 deleted_at
プラグインを構築する場合、他の開発者があなたの機能の一部を変更できることを確認する必要があります。たとえば、リスト内の記事を選択して記事を公開できるブログプラグインがあります。新しい記事が公開されたことを示すためにイベントをトリガーするのが最善です。別の開発者はこのイベントにマウントし、電子メールで購読者に通知できます!
class Posts extends Controller { public function index_onPublish() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $postId) { if ((!$post = Post::find($postId)) || !$post->canEdit($this->user)) continue; $post->publish(); Event::fire('rainlab.blog.posts.published', [$post]); } Flash::success('Successfully published those posts.'); } return $this->listRefresh(); } }
Event::listen('rainlab.blog.posts.published', function($post) { User::subscribedTo($post)->each(function($user) use($post) { Mail::send('emails.notifications.post-published', ['user' => $user, 'post' => $post], function($message) use($user, $post) { $message->from('us@example.com', 'New post by ' . $user->name); $message->to($user->email); }); }); });
rainLabブログプラグイン
記事リストページで、記事を削除できます。しかし、それらをそっと削除したい場合はどうなりますか?これを行うことができるかどうかを見て、10月のスケーラビリティについて詳しく知りましょう。
足場アシスタントコマンドを使用してデモ用の新しいプラグインを作成し、Plugin.phpファイルのプラグインの詳細を更新します。
class Posts extends Controller { public function index_onPublish() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $postId) { if ((!$post = Post::find($postId)) || !$post->canEdit($this->user)) continue; $post->publish(); Event::fire('rainlab.blog.posts.published', [$post]); } Flash::success('Successfully published those posts.'); } return $this->listRefresh(); } }
ソフト削除について話すとき、最初に思い浮かぶのは、データベースに存在する必要があるdeleted_at
フィールド列です。
フォルダーの下にblogplus/updates
という名前の新しいファイルを作成し、create_posts_deleted_at_field.php
ファイルを更新します。 version.yaml
Event::listen('rainlab.blog.posts.published', function($post) { User::subscribedTo($post)->each(function($user) use($post) { Mail::send('emails.notifications.post-published', ['user' => $user, 'post' => $post], function($message) use($user, $post) { $message->from('us@example.com', 'New post by ' . $user->name); $message->to($user->email); }); }); });
php artisan create:plugin rafie.blogplus
テーブルを変更し、rainlab_blog_posts
列を追加します。変更を有効にするために、deleted_at
コマンドを実行することを忘れないでください。 php artisan plugin:refresh rafie.blogplus
# updates/version.yaml 1.0.1: - First version of blogplus. - create_posts_deleted_at_field.php
メソッドに配置する必要があります。 Plugin@boot
メソッドを使用して既存の列を削除することもできます。使用可能な列オプションのリストについては、ドキュメントを確認してください。 removeColumn
# updates/create_posts_deleted_at_field.php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsDeletedAtField extends Migration { public function up() { Schema::table('rainlab_blog_posts', function (Blueprint $table) { $table->timestamp('deleted_at')->nullable()->default(null); }); } public function down() { Schema::table('rainlab_blog_posts', function (Blueprint $table) { $table->dropColumn('deleted_at'); }); } }
属性は、scope
モデルインスタンスで定義されているクエリスコープメソッドの名前である必要があります。 Models\Post
MAGICメソッドを提供します。この例では、スコープフィルターを処理するために記事モデルに新しいメソッドを追加する必要があります。
// plugin.php 在Plugin类的boot方法中 Event::listen('backend.list.extendColumns', function ($widget) { if (!($widget->getController() instanceof \Rainlab\Blog\Controllers\Posts)) { return; } $widget->addColumns([ 'deleted_at' => [ 'label' => 'Deleted', 'type' => 'date', ], ]); });
、addDynamicProperty
などについても同じことができます。記事リストを更新して、変更が機能するかどうかを確認しましょう。 asExtension
もちろん、最後の部分を完了する必要があるため、削除された記事はまだありません。記事の削除操作を傍受し、
ヒント:scope
プロパティを使用する代わりに、条件を使用して、単純な条件を指定できます。次のコードは、モデルスコープを使用するのと同じように機能します。
class Posts extends Controller { public function index_onPublish() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $postId) { if ((!$post = Post::find($postId)) || !$post->canEdit($this->user)) continue; $post->publish(); Event::fire('rainlab.blog.posts.published', [$post]); } Flash::success('Successfully published those posts.'); } return $this->listRefresh(); } }
雄弁さは、各操作に関する一連のイベント(作成、更新、削除など)をトリガーします。この場合、削除イベントにフックし、レコードの削除を防ぐ必要があります。
レコードを削除すると、実際の削除操作が実行される前にイベントがトリガーされ、その後deleting
イベントがトリガーされます。 deleted
イベントでfalseを返すと、操作は中止されます。 deleting
Event::listen('rainlab.blog.posts.published', function($post) { User::subscribedTo($post)->each(function($user) use($post) { Mail::send('emails.notifications.post-published', ['user' => $user, 'post' => $post], function($message) use($user, $post) { $message->from('us@example.com', 'New post by ' . $user->name); $message->to($user->email); }); }); });
結論
10月の拡張とソフト削除プラグインの構築に関するFAQ
deleted_at
ソフト削除とハード削除の違いはどうですか?
10月にソフト削除関数を実装する方法は?
特性を使用することが含まれます。その後、モデルのdeleted_at
メソッドを使用して、レコードを柔らかく削除し、SoftDeletes
メソッドを使用して回復できます。 delete
restore
10月のソフト削除関数をテストする方法は?
既存のレコードでソフト削除関数を使用できますか?
値が削除されていないことを示します。 deleted_at
ソフト削除されたレコードを回復するには、モデルでrestore
メソッドを使用できます。これにより、レコードからdeleted_at
タイムスタンプが削除され、効果的に「整理」します。
はい、モデルのforceDelete
メソッドを使用してソフト削除されたレコードを永続的に削除できます。これにより、ハード削除のようにデータベースからレコードが削除されます。
ソフト削除レコードを含むすべてのレコードを表示するには、モデルでwithTrashed
メソッドを使用できます。これにより、ソフト削除されているかどうかにかかわらず、すべてのレコードが返されます。
deleted_at
メソッドを上書きすることにより、getDeletedAtColumn
列の名前をカスタマイズできます。 deleted_at
がお客様のニーズに適していない場合、これにより、異なる列名を使用できます。 deleted_at
メソッドを使用して、一部のレコードに対してソフト削除を無効にすることができます。これにより、Soft Delete機能から特定のレコードを除外できます。 withoutGlobalScope
以上が10月の拡張 - ソフトディレットプラグインを構築しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。