iT邦幫忙

2021 iThome 鐵人賽

DAY 7
0
Software Development

跟著官方文件學習Laravel, 並實作出一個會員登入系統系列 第 7

Day7 跟著官方文件學習Laravel-開始學習Command用法

註冊的方式我想使用laravel的command來實作,原本想用form表單來實現,不過這樣感覺主題就重複了,剛好用command也可以多學習一個技能,那今天的目標是利用指令輸入註冊的資料,然後讓程式處理後回傳一些輸入後的資訊給使用者看

首先使用指令來生成一個Command吧

php artisan make:command SignUp

生成了一個檔案app/Console/Commands/SignUp.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SignUp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        return 0;
    }
}

這個檔案中有幾項設定

  • $signature這個變數是拿來設定你的指令怎麼打,需要什麼參數等等。
  • $description是拿來寫一些描述
  • handle()這個方法在指令確認無誤後會進來這個方法中

來開始設定囉

# 註冊需要輸入帳號跟密碼還有名稱
protected $signature = 'sign-up {account} {password} {name}';
protected $description = '註冊用的指令';

接著我們設定handle(),利用$this->argument('account')來接傳入的值

public function handle()
    {
        $account = $this->argument('account');
        $password = $this->argument('password');
        $username = $this->argument('username');
        return 0;
    }

接著我們來加入一些判斷,例如密碼需要6位數以上英數字。
利用Validator::make 來達成

public function handle()
    {
        $account = $this->argument('account');
        $password = $this->argument('password');
        $username = $this->argument('username');
        $validator = Validator::make(['password' => $password], [
            'password' => 'regex:' . SignUp::PASSWORD_REGEX
        ]);

        if ($validator->fails()) {
            $this->error("密碼需要6位數以上,並且至少包含大寫字母、小寫字母、數字、符號各一");
            return 1;
        }
        return 0;
    }

而輸出文字可以用$this->error, $this->line來使用

public function handle()
    {
        $account = $this->argument('account');
        $password = $this->argument('password');
        $username = $this->argument('username');
        $validator = Validator::make(['password' => $password], [
            'password' => 'regex:' . SignUp::PASSWORD_REGEX
        ]);

        if ($validator->fails()) {
            $this->error("密碼需要6位數以上,並且至少包含大寫字母、小寫字母、數字、符號各一");
            return 1;
        }
        $this->line("您輸入的帳號:$account");
        $this->line("您輸入的密碼:$password");
        $this->line("您輸入的姓名:$username");
        return 0;
    }

搞定啦,我們利用指令輸入註冊的資料,然後讓程式處理後回傳一些輸入後的資訊給使用者看,明天我們試著把輸入的資料存進資料庫,完成註冊吧!


上一篇
Day6 跟著官方文件學習Laravel-DB設定
下一篇
Day8 跟著官方文件學習Laravel-註冊帳號
系列文
跟著官方文件學習Laravel, 並實作出一個會員登入系統30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
linteddy
iT邦新手 5 級 ‧ 2021-10-14 14:03:17

不好意思我想請問一下
我的SignUp::PASSWORD_REGEX PASSWORD_REGEX底下紅色線

我要留言

立即登入留言