注册的方式我想使用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;
}
}
这个档案中有几项设定
来开始设定罗
# 注册需要输入帐号跟密码还有名称
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;
}
搞定啦,我们利用指令输入注册的资料,然後让程序处理後回传一些输入後的资讯给使用者看,明天我们试着把输入的资料存进资料库,完成注册吧!
<<: Day 21 Azure machine learning: Upload data- 自己的资料自己传
在 Day 06 和 Day 07 中,我们认识了 React.js 的两个 Components...
当你设置完关键字等广告,接着可以使用品质分数这个工具去观察,好让你可以适度做些调整,透过这些品质分数...
Pass Salesforce Omni Studios accreditation on the ...
第 31 天:初始化 LINE BOT on Heroku 事实说来可笑:别试着和任何人讨论任何关於...
Mikrotik RouterOS从入门到实战系列-Mikrotik入门第三课 CAPsMAN无线控...