yii 2.0 admin create 无法在 ActiveForm 中运行
yii 2.0 admin create is not working from ActiveForm
好的,首先我使用的是 Yii 2 基础版,而不是高级版。
好的,我已经做到了,这样我的登录功能就可以从数据库中提取数据,而不是对用户进行硬编码。然后我在我的索引视图中显示我数据库中所有管理员的网格视图及其 ID。我尝试创建一个创建页面,管理员可以在其中创建另一个管理员。
我确实设法让它工作了,但是我终其一生都不知道我做了什么让这个功能停止工作。
Models\User.php
<?php
namespace app\models;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\data\ActiveDataProvider;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $authkey
* @property string $password
* @property string $accessToken
*/
class User extends ActiveRecord implements IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Users';
}
public function rules(){
return [
[['username','password'], 'required']
];
}
public static function findAdmins(){
$query = self::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return static::findOne('AuthKey');
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return static::findOne(['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;
}
}
Views\admin\index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
$this->title = 'Admins';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Admin', ['create'], ['class' => 'btn btn-success']);?>
</p>
<?php
if(isset($user_id)){
print_r($user_id);
}
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'username',
'password',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
所以这可以正常工作并连接到我的数据库。它显示正确的列等。
现在我将向您展示两个不起作用的视图,一个 create.php 呈现 _form.php
views\admin\create.php:
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
$this->title = 'Create Admin';
$this->params['breadcrumbs'][] = ['label' => 'admin', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
if(isset($username)){
print_r($username);
echo '</br>';
print_r($password);
}
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form',[
'model' => $model
]);
?>
</div>
views\admin_form.php:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $model app\models\Users */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="admin-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(); ?>
<?= $form->field($model, 'password')->textInput(); ?>
<div class="form-group">
<?= Html::submitButton('create', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
现在终于是我的控制器了:
controllers\AdminController.php:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\User;
class AdminController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$searchModel = new User;
$dataProvider = $searchModel->findAdmins();
return $this->render('index',[
'dataProvider' => $dataProvider
]);
}
public function actionCreate(){
$model = new User;
$request = Yii::$app->request;
$post = $request->post();
$username = "usernameStandard";
$password = "passwordStandard";
if($model->load(Yii::$app->request->post())){
$username = $post['User']['username'];
$password = $post['User']['password'];
}
return $this->render('create',[
'model' => $model,
'username' => $username,
'password' => $password
]);
}
}
好吧,那么当我尝试这样做时,我填写了来自 create.php 的表格(呈现 _form.php)。当我点击创建按钮时。我 print_r() post 结果以确保它没问题,它确实使用 post 结果再次呈现页面。但是它不会在我的数据库中创建新用户。
真的卡在这里了有人能帮忙吗?
好吧,您应该简单地保存您的模型...例如:
public function actionCreate()
{
$model = new User;
if($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('create',[
'model' => $model,
]);
}
阅读更多http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#save()-detail
好的,首先我使用的是 Yii 2 基础版,而不是高级版。
好的,我已经做到了,这样我的登录功能就可以从数据库中提取数据,而不是对用户进行硬编码。然后我在我的索引视图中显示我数据库中所有管理员的网格视图及其 ID。我尝试创建一个创建页面,管理员可以在其中创建另一个管理员。
我确实设法让它工作了,但是我终其一生都不知道我做了什么让这个功能停止工作。
Models\User.php
<?php
namespace app\models;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\data\ActiveDataProvider;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $authkey
* @property string $password
* @property string $accessToken
*/
class User extends ActiveRecord implements IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Users';
}
public function rules(){
return [
[['username','password'], 'required']
];
}
public static function findAdmins(){
$query = self::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return static::findOne('AuthKey');
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return static::findOne(['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;
}
}
Views\admin\index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
$this->title = 'Admins';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Admin', ['create'], ['class' => 'btn btn-success']);?>
</p>
<?php
if(isset($user_id)){
print_r($user_id);
}
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'username',
'password',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
所以这可以正常工作并连接到我的数据库。它显示正确的列等。
现在我将向您展示两个不起作用的视图,一个 create.php 呈现 _form.php
views\admin\create.php:
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
$this->title = 'Create Admin';
$this->params['breadcrumbs'][] = ['label' => 'admin', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
if(isset($username)){
print_r($username);
echo '</br>';
print_r($password);
}
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form',[
'model' => $model
]);
?>
</div>
views\admin_form.php:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $model app\models\Users */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="admin-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(); ?>
<?= $form->field($model, 'password')->textInput(); ?>
<div class="form-group">
<?= Html::submitButton('create', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
现在终于是我的控制器了:
controllers\AdminController.php:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\User;
class AdminController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$searchModel = new User;
$dataProvider = $searchModel->findAdmins();
return $this->render('index',[
'dataProvider' => $dataProvider
]);
}
public function actionCreate(){
$model = new User;
$request = Yii::$app->request;
$post = $request->post();
$username = "usernameStandard";
$password = "passwordStandard";
if($model->load(Yii::$app->request->post())){
$username = $post['User']['username'];
$password = $post['User']['password'];
}
return $this->render('create',[
'model' => $model,
'username' => $username,
'password' => $password
]);
}
}
好吧,那么当我尝试这样做时,我填写了来自 create.php 的表格(呈现 _form.php)。当我点击创建按钮时。我 print_r() post 结果以确保它没问题,它确实使用 post 结果再次呈现页面。但是它不会在我的数据库中创建新用户。
真的卡在这里了有人能帮忙吗?
好吧,您应该简单地保存您的模型...例如:
public function actionCreate()
{
$model = new User;
if($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('create',[
'model' => $model,
]);
}
阅读更多http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#save()-detail