- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我开始迈出 Yii2 的第一步。到目前为止,我已经能够编写应用程序并将数据库中的表连接到它,就像我在 Yii1 中学到的那样。
表是 contacts
,我创建 View 中的表单将数据毫无问题地发送到数据库。
问题是我只能在 Yii2 内置的静态用户模型中使用 Admin/Admin 或 demo/demo 登录。
在 Yii1.xx 中,我设法使用 COMPONENTS/useridentity 从数据库中的表验证登录,就像这样(我使用了一个名为 utilizador
的表,其中包含字段 id
, utilizador
和 password
):
class UserIdentity extends CUserIdentity
{
public function authenticate() {
$conexao=Yii::app()->db;
$consulta="select utilizador,password from utilizador ";
$consulta.="where utilizador='".$this->username."' and ";
$consulta.="password='".$this->password."'";
$resultado=$conexao->createCommand($consulta)->query();
$resultado->bindColumn(1,$this->username);
$resultado->bindColumn(2,$this->password);
while($resultado->read()!==false){
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}
}
对于 Yii2,我已经阅读了很多教程,包括 Stack Overflow 中的一个,但没有一个教程告诉我如何使用 MySQL 数据库中的用户名和密码登录用户。 Yii2 没有 components/useridentity.php,我不知道从哪里开始,也不知道什么是正确的代码来覆盖开箱即用的静态用户模型。
我也尝试过扩展 Yii2-user,阅读 PDF 指南但不明白如何从我的 Controller 中的 vendor 文件夹调用路由。做了几次尝试,但都失败了。
谁能教我如何在 Yii2 中验证数据库登录,最好不使用扩展?
编辑
我在 Stack Overflow 中阅读了这个教程 Yii Framework 2.0 Login With User Database
还研究了 Yii2 用户扩展的 PDF,但不知道如何处理 pdf 的后续部分和下一部分。它谈到路线,但我不知道如何使用它们:
2.3.1 显示用户路由/user/admin/index 显示注册用户列表。您将能够看到很多有用的信息,例如注册时间和 ip 地址、确认和阻止状态等。
我也读过这个: http://www.yiiframework.com/doc-2.0/yii-web-user.html但我认为它没有解决我的问题的步骤。
编辑 2
我尝试在 Yii 基本模板中实现用户模型和登录表单模型来验证用户登录。创建了一个数据库并连接到它。数据库作为表用户和字段用户名、密码、authKey、acessToken 填充值。从 ActiveRecord 扩展用户模型并实现\yii\web\IdentityInterface 以使内置的 Yii2 函数完成它们的工作。还编写了方法 public static function tableName() { return 'user';
每次我尝试登录时,都会从 LoginForm 模型中的 validatepassword() 抛出 -> username or password incorrect。
这是我的代码:登录表单模型:
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
...这是我的 User.php 模型
<?php
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public static function tableName() { return 'user'; }
/**
* @inheritdoc
*/
public static function findIdentity($id) {
$user = self::find()
->where([
"id" => $id
])
->one();
if (!count($user)) {
return null;
}
return new static($user);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $userType = null) {
$user = self::find()
->where(["accessToken" => $token])
->one();
if (!count($user)) {
return null;
}
return new static($user);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username) {
$user = self::find()
->where([
"username" => $username
])
->one();
if (!count($user)) {
return null;
}
return new static($user);
}
/**
* @inheritdoc
*/
public function getId() {
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey() {
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey) {
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password) {
return $this->password === $password;
}
}
有什么想法吗?? -> 谢谢...
我不知道我还应该做什么,可能是在验证密码或查找用户名时出现问题,在 Yii2 调试中它显示正确连接到 mysql 数据库。
不要触及 siteController actionLogin(),因为它等同于高级模板,我认为最好保持这种状态。
编辑 3 -> 4 小时弄乱了模型代码,将我阅读的每个解决方案付诸实践,但它仍然抛出“不正确的用户名或密码”。来自:
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
我不想放弃,但我正在考虑回到旧的 Yii1.xx。在那里我可以轻松地查询数据库并使一个良好的登录系统正常工作。
最佳答案
Yii2 高级应用程序默认带有一个来自数据库的登录部分的工作示例(我看到基本的使用静态用户名和密码)。您不必安装任何额外的东西,只需查看代码即可。安装高级应用程序并查看前端。
简而言之,SiteController 使用 LoginModel 进行验证,然后使用 LoginModel 的 login() 将 User 模型登录到 User 组件。
如果您不想使用用户模型,只需创建您自己的模型并使用该模型即可。您不想使用默认的用户组件,只需创建您自己的组件即可。这很容易做到。
编辑:伙计,删除下面变量的公共(public)声明。
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
你告诉 Yii 忽略数据库中的内容。
关于php - Yii2 分步指南从 MySQL 中的表登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304066/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!