Laravel11の公式パッケージ Prompts の使い方の記録と解説の第2回です。
![](https://laravel.com/img/og-image.jpg)
前回はピュアPHPでtextプロンプトを実装しました。
今回はピュアPHPでpassword、confirm、select、multiselectのプロンプトを実装していきます。
passwordプロンプトの使い方
passwordはそのまんまパスワード入力用のプロンプトです。
▼基本的な使い方
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use function Laravel\Prompts\password;
$password = password('パスワードを入力してください。');
echo "認証しました。\n";
▼実行結果
ラベルとテキストボックスが表示されるところまではtextプロンプトと同じです。
![](http://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_01.png)
入力した内容が伏字となります。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_02.png)
入力した内容は変数$passwordに格納されていますが
今回は表示しません。
▼プレイスホルダー、補足説明、必須、バリデーション
$password = password(
label: 'パスワードを入力してください。',
placeholder: 'password',
hint: '半角8文字以上',
required: 'パスワードは入力必須です。',
validate: fn (string $value) => match (true) {
strlen($value) < 8 => 'パスワードは半角8文字以上です。',
default => null
}
);
▼実行結果
プレイスホルダーと補足説明が表示されました。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_03.png)
未入力で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_04.png)
8文字未満で確定しても叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_05.png)
confirmプロンプトの使い方
confirmは確認ダイアログのような役割をします。
「利用規約に同意しますか?」という質問に対して
「同意する」「同意しない」のような選択肢を選ばせます。
▼基本的な使い方(src/confirm.php)
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use function Laravel\Prompts\confirm;
$confirmed = confirm('利用規約に同意しますか?');
var_dump($confirmed);
▼実行結果
ラベルとデフォルトの選択肢が表示されます。
矢印キー左右[←][→]で選択肢移動し[Enter]で確定します。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_06.png)
「Yes」で「true」、「No」で「false」が返ります。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_07.png)
▼デフォルト、選択肢、補足説明、必須
$confirmed = confirm(
label: '利用規約に同意しますか?',
default: false,
yes: '同意する',
no: '同意しない',
hint: '利用規約に同意する必要があります。',
required: '利用規約に同意しなければなりません。'
);
▼実行結果
ラベルと選択肢、補足説明が表示され、
デフォルトの「同意しない」(false)が選択されています。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_08.png)
「同意しない」で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_09.png)
selectプロンプトの使い方
selectは複数選択肢から単一選択させるプロンプトです。
▼基本的な使い方
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use function Laravel\Prompts\select;
$selected = select(
'コマンド',
['たたかう', 'じゅもん', 'ぼうぎょ', 'どうぐ', 'さくせん', 'にげる'],
);
echo "コマンド:{$selected}\n";
▼実行結果
ラベルと選択肢が表示されます。
選択肢が5個を超える場合はスクロールバーが表示されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_10.png)
矢印キーの上下[↑][↓]で選択し[Enter]で確定します。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_11.png)
選択した内容が$selectedに格納されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_12.png)
▼デフォルト、補足説明、スクロール
$selected = select(
label: 'コマンド',
options: ['たたかう', 'じゅもん', 'ぼうぎょ', 'どうぐ', 'さくせん', 'にげる'],
default: 'ぼうぎょ',
hint: 'どうする?',
scroll: 6,
);
▼実行結果
補足説明、選択肢6個がスクロールバー無しで表示され、
デフォルト「ぼうぎょ」が選択されています。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_13.png)
▼選択肢の代入値と表示値の分離
※ハッシュキーが代入値、値が表示値です。
$selected = select(
label: 'コマンド',
options: [
'battle' => 'たたかう',
'spell' => 'じゅもん',
'defence' => 'ぼうぎょ',
'tools' => 'どうぐ',
'strategy' => 'さくせん',
'escape' => 'にげる'
],
default: 'defence',
hint: 'どうする?',
scroll: 6,
);
▼実行結果
表示される選択肢は日本語ですが、
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_14.png)
選択した結果$selectedに代入される値は英語です。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_15.png)
▼バリデーション
$selected = select(
label: 'コマンド',
options: [
'battle' => 'たたかう',
'spell' => 'じゅもん',
'defence' => 'ぼうぎょ',
'tools' => 'どうぐ',
'strategy' => 'さくせん',
'escape' => 'にげる'
],
default: 'defence',
hint: 'どうする?',
scroll: 6,
validate: fn (string $value) => match ($value) {
'spell' => 'じゅもんをおぼえていない!',
'tools' => 'どうぐをもっていない!',
'strategy' => 'さくせんがおもいつかない!',
'escape' => 'にげられない!',
default => null,
}
▼実行結果
じゅもんをおぼえていない!
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_16.png)
どうぐをもっていない!
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_17.png)
さくせんがおもいつかない!
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_18.png)
にげられない!
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_19.png)
multiselectプロンプトの使い方
multiselectは複数選択肢から複数選択させるプロンプトです。
▼基本的な使い方
<?php
require_once(__dir__ . '/../vendor/autoload.php');
use function Laravel\Prompts\multiselect;
$items = multiselect(
'なにをおもとめですか?',
['やくそう', 'どくけしそう', 'まんげつそう', 'おなべのふた', 'キメラのつばさ']
);
var_dump($items);
▼実行結果
ラベルと選択肢が表示されました。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_20.png)
矢印キー上下[↑][↓]でカーソル移動、
スペースキーで選択、[Enter]で確定します。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_21.png)
選択した内容が配列として$itemsに格納されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_22.png)
▼デフォルト、補足説明、スクロール、必須
$items = multiselect(
label: 'なにをおもとめですか?',
options: ['やくそう', 'どくけしそう', 'まんげつそう', 'おなべのふた', 'ステテコパンツ', 'キメラのつばさ'],
default: ['やくそう', 'おなべのふた'],
hint: 'かうものをえらんでください。',
scroll: 6,
required: 'なにかかってくださいよ。まったくもう。',
);
▼実行結果
デフォルトが選択され、補足説明が表示されています。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_23.png)
選択解除した状態で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_24.png)
▼選択肢の代入値と表示値の分離
selectと同様です。
$items = multiselect(
label: 'なにをおもとめですか?',
options: [
'medicinal herb' => 'やくそう',
'antidote herb' => 'どくけしそう',
'full moon herb' => 'まんげつそう',
'pot lid' => 'おなべのふた',
'steteco pants' => 'ステテコパンツ',
'chimera wings' => 'キメラのつばさ',
],
default: ['medicinal herb', 'pot lid'],
hint: 'かうものをえらんでください。',
scroll: 6,
required: 'なにかかってくださいよ。まったくもう。',
);
▼実行結果
selectと同様です。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_25.png)
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_26.png)
▼バリデーション
$items = multiselect(
label: 'なにをおもとめですか?',
options: [
'medicinal herb' => 'やくそう',
'antidote herb' => 'どくけしそう',
'full moon herb' => 'まんげつそう',
'pot lid' => 'おなべのふた',
'steteco pants' => 'ステテコパンツ',
'chimera wings' => 'キメラのつばさ',
],
default: ['medicinal herb', 'pot lid'],
hint: 'かうものをえらんでください。',
scroll: 6,
required: 'なにかかってくださいよ。まったくもう。',
validate: fn (array $values) => !in_array('medicinal herb', $values)
? '「やくそう」はかってくださいよ。'
: null
);
▼実行結果
「やくそう」の選択を外すと叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/04/laravel11_prompts_purephp_password_27.png)
今回は以上です。
次回はpause、info、table、spin、progressの使い方です。
コメント