laravel5.3开发知乎-第1章-注册 laravel5.3开发知乎-第1章-注册

2023-07-07

一、下载 laravel 5.3

composer create-project laravel/laravel=5.3.* laravel5.3_zh

新建数据库 laravel5.3_zh

修改 .evn 配置文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.3_zh
DB_USERNAME=laravel5.3_zh
DB_PASSWORD=laravel5.3_zh

修改中国时区,在 config/app.php 中修改

'timezone' => 'PRC',

切换目录

cd laravel5.3_zh

二、数据准备

修改迁移文件:user.php

public function up()
{
   Schema::create('users', function (Blueprint $table) {
       $table->increments('id');
       $table->string('name')->unique();
       $table->string('email')->unique();
       $table->string('password', 60);
       $table->string('avatar');
       $table->string('confirmation_token');
       $table->smallInteger('is_active')->default(0);
       $table->integer('questions_count')->default(0);
       $table->integer('answers_count')->default(0);
       $table->integer('comments_count')->default(0);
       $table->integer('favorites_count')->default(0);
       $table->integer('likes_count')->default(0);
       $table->integer('followers_count')->default(0);
       $table->integer('followings_count')->default(0);
       $table->json('setting')->nullable();
       $table->rememberToken();
       $table->timestamps();
   });
}

执行数据迁移

php artisan migrate

三、下载 naux/sendcloud

注册 sendcloud:https://www.sendcloud.net/,创建 API_KEY,创建模板:zhihu_app_register

下载 naux/sendcloud,github:https://github.com/nauxliu/Laravel-SendCloud

composer.json 的 require 添加

"guzzlehttp/guzzle": "~6.0",
"naux/sendcloud": "1.1.*"

下载

composer update

配置 providers,config/app.php 的 providers 添加

\Naux\Mail\SendCloudServiceProvider::class,

修改 .env

MAIL_DRIVER=sendcloud

SEND_CLOUD_USER=   # 创建的 api_user
SEND_CLOUD_KEY=    # 分配的 api_key

四、注册用户

make:auth

php artisan make:auth

启动 serve

php artisan serve

新建默认头像:public/images/avatars/default.png

注册路由:修改 routes/web.php

Route::get('email/verify/{token}',['as'=>'email.verify','uses'=>'EmailController@verify']);

生成控制器

php artisan make:controller EmailController

修改生成的控制:app/Http/Controllers/EmailController

use App\User;
use Auth;

public function verify($token)
{
   $user = User::where('confirmation_token', $token)->first();
   if(is_null($user)){
       return redirect('/');
   }
   $user->is_active = 1;
   $user->confirmation_token = str_random(40);
   $user->save();
   Auth::login($user);
   return redirect('/home');
}

修改 user 模型的 fillable

protected $fillable = [
   'name', 'email', 'password', 'avatar', 'confirmation_token', 'setting'
];

修改 app/Http/Controllers/RegisterController.php

use Naux\Mail\SendCloudTemplate;
use Mail;

protected function create(array $data)
{
   $user = User::create([
       'name' => $data['name'],
       'email' => $data['email'],
       'avatar' => '/images/avatars/default.png',
       'confirmation_token' => str_random(40),
       'password' => bcrypt($data['password']),
       'setting' => ['city'=>''],
   ]);
   //$this->sendVerifyEmailTo($user);
   return $user;
}

private function sendVerifyEmailTo($user)
{
   $data = [
       'url' => route('email.verify',['token'=>$user->confirmation_token]),
       'name' => $user->name
   ];
   $template = new SendCloudTemplate('zhihu_app_register', $data);
   Mail::raw($template, function($message) use ($user){
       $message->from('alulubin@gmail.com', 'lulublog');
       $message->to($user->email);
   });
}

自定义错误提示:修改 resources/lang/en/validation.php

'custom' => [
   'email' => [
       'unique' => '邮箱已被占用',
   ],
   'password' => [
       'confirmed' => '两次输入的密码不一致',
   ],
],

访问:http://localhost:8000/register 注册用户

PS:由于 sendCloud 接口已更新,而 naux/sendcloud 没有更新,所以发送不了邮件。

五、使用 qq 邮箱

在此之前先确认QQ邮箱是否开启了POP3/SMTP服务,如果未开启则须开启:QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。

获取授权码:QQ邮箱->设置->账户->获取授权码(保存起来)

修改 .evn 配置文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=452936616@qq.com
MAIL_PASSWORD=xxxxxxxxxxx
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=452936616@qq.com
MAIL_FROM_NAME=lulubin

注意:MAIL_FROM_ADDRESS 和 MAIL_FROM_NAME 分别配置了发送者的邮件地址和名字

如果 port 为 587,则 encryption 为 tls

MAIL_PORT=587
MAIL_ENCRYPTION=tls

设置后记得重启 serve

六、创建邮件模板

php artisan make:mail WelcomeToLulublog

修改创建成功的文件:app/Mail/WelcomeToLulublog.php 的 build 方法

public function build()
{
   return $this->view('email.welcome');
}

新建视图

resources/views/email/welcome.blade.php

https://file.lulublog.cn/images/3/2023/07/wzd1LOV5790i88S2717G9x2D0bI7ov.jpg

修改 app/Http/Controllers/RegisterController.php

protected function create(array $data)
{
   $user = User::create([
       'name' => $data['name'],
       'email' => $data['email'],
       'avatar' => '/images/avatars/default.png',
       'confirmation_token' => str_random(40),
       'password' => bcrypt($data['password']),
       'api_token' => str_random(60),
   ]);
   $this->sendVerifyEmailTo($user);
   flash('已发送激活邮件到您的注册邮箱,请前往激活')->success();
   return $user;
}

private function sendVerifyEmailTo($user)
{
   $data = [
       'url' => route('email.verify',['token'=>$user->confirmation_token]),
       'name' => $user->name
   ];
   $subject = config('app.name').'注册';
   Mail::to($user->email)->send(new \App\Mail\WelcomeToLulublog($data));
   //count(Mail::failures())
}

private function sendVerifyEmailToOld($user)
{
   $data = [
       'url' => route('email.verify',['token'=>$user->confirmation_token]),
       'name' => $user->name
   ];
   Mail::send('email.welcome', $data, function ($message) use($user){
       $subject = config('app.name').'注册';
       $message->to($user->email)->subject($subject);
   });
   //count(Mail::failures())
}

访问:http://localhost:8000/register 注册用户

阅读 437