yii2mod/yii2-user
Composer 安装命令:
composer require yii2mod/yii2-user
包简介
User module
README 文档
README
Yii2 User Extension
Flexible user registration and authentication module for Yii2
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-user "*"
or add
"yii2mod/yii2-user": "*"
to the require section of your composer.json file.
Actions
This extension provides several independent action classes, which provides particular operation support:
- [[yii2mod\user\actions\LoginAction]] - Logs in a user. The following additional parameters are available:
view- name of the view, which should be rendered.modelClass- login model class name.layout- the name of the layout to be applied to this view.returnUrl- url which user should be redirected to on success.
- [[yii2mod\user\actions\LogoutAction]] - Logs out the current user. The following additional parameters are available:
returnUrl- url which user should be redirected to on success.
- [[yii2mod\user\actions\SignupAction]] - Signup a user. The following additional parameters are available:
view- name of the view, which should be rendered.modelClass- signup model class name.returnUrl- url which user should be redirected to on success.
- [[yii2mod\user\actions\RequestPasswordResetAction]] - Request password reset for a user. The following additional parameters are available:
view- name of the view, which should be rendered.modelClass- request password model class.successMessage- message to the user when the mail is sent successfully.errorMessage- error message for the user when the email was not sent.returnUrl- url which user should be redirected to on success.
- [[yii2mod\user\actions\PasswordResetAction]] - Reset password for a user. The following additional parameters are available:
view- name of the view, which should be rendered.modelClass- reset password model class.successMessage- message to be set on success.returnUrl- url which user should be redirected to on success.
Configuration
- If you use this extension without base template, then you need execute migration by the following command:
php yii migrate/up --migrationPath=@vendor/yii2mod/yii2-user/migrations
- You need to configure the
paramssection in your project configuration:
'params' => [ 'user.passwordResetTokenExpire' => 3600 ]
- Your need to create the UserModel class that be extends of UserModel and configure the property
identityClassforusercomponent in your project configuration, for example:
'user' => [ 'identityClass' => 'yii2mod\user\models\UserModel', // for update last login date for user, you can call the `afterLogin` event as follows 'on afterLogin' => function ($event) { $event->identity->updateLastLogin(); } ],
-
For sending emails you need to configure the
mailercomponent in the configuration of your project. -
If you don't have the
passwordResetToken.phptemplate file in the mail folder of your project, then you need to create it, for example:
<?php use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $user */ $resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/password-reset', 'token' => $user->password_reset_token]); ?> Hello <?php echo Html::encode($user->username) ?>, Follow the link below to reset your password: <?php echo Html::a(Html::encode($resetLink), $resetLink) ?>
This template used for password reset email.
- Add to SiteController (or configure via
$routeparam in urlManager):
/** * @return array */ public function actions() { return [ 'login' => [ 'class' => 'yii2mod\user\actions\LoginAction' ], 'logout' => [ 'class' => 'yii2mod\user\actions\LogoutAction' ], 'signup' => [ 'class' => 'yii2mod\user\actions\SignupAction' ], 'request-password-reset' => [ 'class' => 'yii2mod\user\actions\RequestPasswordResetAction' ], 'password-reset' => [ 'class' => 'yii2mod\user\actions\PasswordResetAction' ], ]; }
You can then access to this actions through the following URL:
- http://localhost/site/login
- http://localhost/site/logout
- http://localhost/site/signup
- http://localhost/site/request-password-reset
- http://localhost/site/password-reset
- Also some actions send flash messages, so you should use an AlertWidget to render flash messages on your site.
Using action events
You may use the following events:
/** * @return array */ public function actions() { return [ 'login' => [ 'class' => 'yii2mod\user\actions\LoginAction', 'on beforeLogin' => function ($event) { // your custom code }, 'on afterLogin' => function ($event) { // your custom code }, ], 'logout' => [ 'class' => 'yii2mod\user\actions\LogoutAction', 'on beforeLogout' => function ($event) { // your custom code }, 'on afterLogout' => function ($event) { // your custom code }, ], 'signup' => [ 'class' => 'yii2mod\user\actions\SignupAction', 'on beforeSignup' => function ($event) { // your custom code }, 'on afterSignup' => function ($event) { // your custom code }, ], 'request-password-reset' => [ 'class' => 'yii2mod\user\actions\RequestPasswordResetAction', 'on beforeRequest' => function ($event) { // your custom code }, 'on afterRequest' => function ($event) { // your custom code }, ], 'password-reset' => [ 'class' => 'yii2mod\user\actions\PasswordResetAction', 'on beforeReset' => function ($event) { // your custom code }, 'on afterReset' => function ($event) { // your custom code }, ], ]; }
Console commands
Setup
To enable console commands, you need to add module into console config of you app.
/config/console.php in yii2-app-basic template, or /console/config/main.php in yii2-app-advanced.
return [ 'id' => 'app-console', 'modules' => [ 'user' => [ 'class' => 'yii2mod\user\ConsoleModule', ], ],
Available console actions
- user/create - Creates a new user.
./yii user/create <email> <username> <password> - email (required): string - username (required): string - password (required): string
- user/role/assign - Assign role to the user.
./yii user/role/assign <roleName> <email> - roleName (required): string - email (required): string
- user/role/revoke - Revoke role from the user.
./yii user/role/revoke <roleName> <email> - roleName (required): string - email (required): string
- user/delete - Deletes a user.
./yii user/delete <email> - email (required): string
- user/update-password - Updates user's password to given.
./yii user/update-password <email> <password> - email (required): string - password (required): string
Internationalization
All text and messages introduced in this extension are translatable under category 'yii2mod.user'. You may use translations provided within this extension, using following application configuration:
return [ 'components' => [ 'i18n' => [ 'translations' => [ 'yii2mod.user' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@yii2mod/user/messages', ], // ... ], ], // ... ], // ... ];
Support us
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
yii2mod/yii2-user 适用场景与选型建议
yii2mod/yii2-user 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.5k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2014 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「User management」 「yii2」 「yii2-user」 「user module」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2mod/yii2-user 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2mod/yii2-user 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2mod/yii2-user 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Silverstripe module to allow you to log into any of your Silverstripe sites from one place.
Task system for APPUI
Standalone replacement for php's native get_browser() function
User component for Yii2
SCEditor, a lightweight WYSIWYG BBCode & HTML editor extension for Yii 2.0 Framework
统计信息
- 总下载量: 17.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-26