Readouble

Livewire v3 プロパティの変更ロック

プロパティの変更ロック

Livewireのプロパティは、wire:modelのようなユーティリティを使用して、フロントエンドとバックエンドの両方で自由に修正できます。モデルIDのようなプロパティがフロントエンドで変更されるのを防ぎたい場合は、Livewireの#[Locked]属性を使用できます。Livewire properties are able to be modified freely on both the frontend and backend using utilities like wire:model. If you want to prevent a property — like a model ID — from being modified on the frontend, you can use Livewire's #[Locked] attribute.

基本的な使用法Basic usage

以下は、PostモデルのIDを$idという名前のパブリックプロパティとして格納するShowPostコンポーネントです。このプロパティが好奇心旺盛なユーザーや悪意のあるユーザーによって変更されるのを防ぐために、#[Locked]属性をプロパティに追加できます。Below is a ShowPost component that stores a Post model's ID as a public property named $id. To keep this property from being modified by a curious or malicious user, you can add the #[Locked] attribute to the property:

warning Warning! 属性クラスをインポートしてください すべての属性クラスをインポートしてください。たとえば、以下の#[Locked]属性は、次のインポートが必要です:use Livewire\Attributes\Locked;[!warning] Make sure you import attribute classes Make sure you import any attribute classes. For example, the below #[Locked] attribute requires the following import use Livewire\Attributes\Locked;.

use Livewire\Attributes\Locked;
use Livewire\Component;

class ShowPost extends Component
{
	#[Locked] // [tl! highlight]
    public $id;

    public function mount($postId)
    {
        $this->id = $postId;
    }

	// ...
}

#[Locked]属性を追加することで、$idプロパティが改ざんされることはありません。By adding the #[Locked] attribute, you are ensured that the $id property will never be tampered with.

lightbulb Tip: モデルプロパティはデフォルトで安全です モデルのIDだけでなく、Eloquentモデルをパブリックプロパティに格納する場合、Livewireは、明示的に#[Locked]属性をプロパティに追加しなくても、IDが改ざんされないようにします。ほとんどの場合、これは#[Locked]を使用するよりも優れた方法です。[!tip] Model properties are secure by default If you store an Eloquent model in a public property instead of just the model's ID, Livewire will ensure the ID isn't tampered with, without you needing to explicitly add the #[Locked] attribute to the property. For most cases, this is a better approach than using #[Locked]:

class ShowPost extends Component
{
   public Post $post; // [tl! highlight]

   public function mount($postId)
   {
       $this->post = Post::find($postId);
   }

  // ...
}

なぜprotectedプロパティを使用しないのですか?Why not use protected properties?

機密データにprotectedプロパティを使用しないのはなぜか?と疑問に思うかもしれません。You might ask yourself: why not just use protected properties for sensitive data?

Livewireは、ネットワークリクエスト間でパブリックプロパティのみを保持することを覚えておいてください。静的なハードコードされたデータの場合、protectedプロパティが適しています。ただし、実行時に格納されるデータの場合、データが適切に保持されるように、パブリックプロパティを使用する必要があります。Remember, Livewire only persists public properties between network requests. For static, hard-coded data, protected properties are suitable. However, for data that is stored at runtime, you must use a public property to ensure that the data is persisted properly.

Livewireはこれを自動的に行えないのですか?Can't Livewire do this automatically?

理想的な世界では、Livewireはデフォルトでプロパティをロックし、wire:modelがそのプロパティで使用されている場合にのみ変更を許可します。In a perfect world, Livewire would lock properties by default, and only allow modifications when wire:model is used on that property.

残念ながら、そのためには、LivewireがすべてのBladeテンプレートを解析して、プロパティがwire:modelまたは同様のAPIによって変更されているかどうかを理解する必要があります。Unfortunately, that would require Livewire to parse all of your Blade templates to understand if a property is modified by wire:model or a similar API.

それは技術的およびパフォーマンスのオーバーヘッドを追加するだけでなく、Alpineやその他のカスタムJavaScriptのようなものでプロパティが変更されたかどうかを検出することは不可能です。Not only would that add technical and performance overhead, it would be impossible to detect if a property is mutated by something like Alpine or any other custom JavaScript.

したがって、Livewireは引き続きパブリックプロパティをデフォルトで自由に変更可能にし、必要に応じてそれらをロックするためのツールを開発者に提供します。Therefore, Livewire will continue to make public properties freely mutable by default and give developers the tools to lock them as needed.

章選択

パッケージ

設定

バージョン変更
linkv3 linkv2
明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
OS表示
全OS表示
macOSのみ表示
windowsのみ表示
linuxのみ表示
JSフレームワーク
両フレームワーク
Reactのみ表示
Vueのみ表示
JSのみ表示

(JSが存在しない場合は、他を全表示)

和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作