composer require topthink/think-migration
php think migrate:create Users
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class Users extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$this->table("users")
->addColumn("name", "string", ["default" => "", 'limit' => 100])
->addColumn("email", "string", ["default" => "", 'limit' => 255])
->addColumn("password", "string", ["default" => "", 'limit' => 255])
->addColumn("remember_token", "string", ["default" => "", 'limit' => 255])
->addTimestamps()
->addIndex(['email'], ['unique' => true])
->save();
}
}
在provider中使用
model
,需要做一下操作
- 创建User模型
- 模型继承
tp5er\think\auth\User
//如果你愿意,除了用户的电子邮件和密码之外,还可以向身份验证查询中添加额外的查询条件。为了实现这一点,我们可以简单地将查询条件添加到传递给 attempt 方法的数组中。
Auth::attempt(['email' => 'tp5er@qq.com', 'password' => '123456'], true);
//您可以将布尔值作为第二个参数传递给 login 方法。此值指示是否需要验证会话的 「记住我」 功能。请记住,这意味着会话将被无限期地验证,或者直到用户手动注销应用程序:
Auth::login(User::find(1), $remember = false);
//只验证一次
Auth::once(['email' => 'tp5er@qq.com', 'password' => '123456']);
//只验证一次通过id
Auth::onceUsingId(1);
// 获取当前的认证用户信息 ...
$user = Auth::user();
// 获取当前的认证用户id ...
$id = Auth::id();
if (Auth::check()) {
// 用户已登录...
}
//使用户退出登录(清除会话)
Auth::logout();
如果你认可我们的开源项目,有兴趣为 think-auth 的发展做贡献,竭诚欢迎加入我们一起开发完善。无论是报告错误或是 Pull Request 开发,那怕是修改一个错别字也是对我们莫大的帮助。