我如何添加新的管理页面到YII


How can I add new admin pages to YII?

我有一个完整的YII,我试图复制一个管理页面,使另一个。admin.php包含如下内容:

<?php
/* @var $this UsersController */
/* @var $model banned */
//$model = 'Users';
$this->breadcrumbs=array(
    'Banned users'=>array('index'),
    'Manage',
);
/*
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});
$('.search-form form').submit(function(){
    $('#banned-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");*/
?>
<h1>Manage Banned Users</h1>
<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search1',array(
    'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'banned-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'uid',
        //'name',
        //'email',
        //'password',
        'datejoined',
        //'picture',
        /*
        'interestingquestionnotify',
        'myquestionnotify',
        'myanswernotify',*/
        'role',
        'status',
        //'newsletternotify',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>
  <?php
 // print_r($model);
  ?>

我的问题是:当我打开菜单点什么显示我这个admin php,比我可以看到其他菜单点的zii.widgets.grid.CGridView。I need to be used the Banned.php从模型文件夹,but this one use Users.php。知道怎么调试它吗?

我已经禁止了模型中的.php。我有禁课。我已经禁止在视图

这是我的protected/controllers/BannedController.php

<?php
class BannedController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout='//layouts/column1';
    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }
    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('index','view','admin','delete','create','update'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
/*  public function actionView($id)
    {
        $model = $this->loadModel($id);
        $questions = new Questions('search');
        $ans = new Answers('search');
        // válaszok
        if(isset($_GET['Answers'])){
            $ans->attributes = $_GET['Answers'];
            $ans->uid = $model->uid;
            $answersDataProvider = $ans->searchOnUser();
        } else {
            $answersDataProvider = new CActiveDataProvider('Answers', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }
        //kérdések
        if(isset($_GET['Questions'])){
            $questions->attributes = $_GET['Questions'];
            $questions->uid = $model->uid;
            $questionsDataProvider = $questions->searchOnUser();
        } else {
            $questionsDataProvider = new CActiveDataProvider('Questions', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }
        $this->render('view',array(
            'model'=>$model,
            'answersDataProvider'=>$answersDataProvider,
            'questionsDataProvider'=>$questionsDataProvider,
            'ans'=>$ans,
            'questions'=>$questions
        ));
    } */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }       
    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Banned;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {
            $model->attributes=$_POST['Users'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {
            $model->attributes=$_POST['Users'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }
    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        DMongo::get()->selectCollection('users')->remove(array('_id' => intval($id)));
        $this->loadModel($id)->delete();
        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('Users');
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }
    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model=new Users('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Users']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $this->render('admin',array(
            'model'=>$model,
        ));
    }
    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Users the loaded model
     * @throws CHttpException
     */ 
    public function loadModel($id)
    {
        $model=Banned::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist1.');
        return $model;
    }
    /**
     * Performs the AJAX validation.
     * @param Users $model the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='users-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}

这是:protected/models/forbidden .php

<?php
/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $uid
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $datejoined
 * @property string $picture
 * @property integer $interestingquestionnotify
 * @property integer $myquestionnotify
 * @property integer $myanswernotify
 * @property string $role
 * @property integer $status
 * @property integer $newsletternotify
 */
class Banned extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Banned the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_banned_users';
    }
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, user_mail, user_name'),
            array('interestingquestionnotify, myquestionnotify, myanswernotify, status, newsletternotify', 'numerical', 'integerOnly'=>true),
            array('name, email, password, picture', 'length', 'max'=>50),
            array('role', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid, name, email, password, datejoined, picture, interestingquestionnotify, myquestionnotify, myanswernotify, role, status, newsletternotify', 'safe', 'on'=>'search'),
        );
    }
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'uid' => 'Azonosító1',
            'name' => 'Név',
            'email' => 'E-mail',
            'password' => 'Jelszó',
            'datejoined' => 'Dátum',
            'picture' => 'Profilkép',
            'interestingquestionnotify' => 'Megfigyelt kérdésekről értesítés',
            'myquestionnotify' => 'Kérdésekről értesítés',
            'myanswernotify' => 'Válaszokról értesítés',
            'role' => 'Jog',
            'status' => 'Állapot',
            'newsletternotify' => 'Hírekről értesítés',
        );
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
        $criteria->compare('uid',$this->uid);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('datejoined',$this->datejoined,true);
        $criteria->compare('picture',$this->picture,true);
        $criteria->compare('interestingquestionnotify',$this->interestingquestionnotify);
        $criteria->compare('myquestionnotify',$this->myquestionnotify);
        $criteria->compare('myanswernotify',$this->myanswernotify);
        $criteria->compare('role',$this->role,true);
        $criteria->compare('status',$this->status);
        $criteria->compare('newsletternotify',$this->newsletternotify);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    public function beforeSave() {
        if ($this->isNewRecord)
            $this->password = md5($this->password);
        return parent::beforeSave();
    }
}

我的BannedController不从forbidden。php获取数据,它从users。php获取现在我将所有用户替换为禁止,但它写:Banned has an invalid validation rule. The rule must specify attributes to be validated and the validator name.

让我们检查一下

在你的actionCreate方法中做如下所示

public function actionCreate()
    {
        $model=new Banned;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['Banned']))
        {
            $model->attributes=$_POST['Banned'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

和在你的actionAdmin方法做如下所示

 public function actionAdmin()
    {
        $model=new Banned('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Banned']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

试一试,看看

检查显示此视图的控制器方法U可能已将User作为模型发送给视图如

所示
$model=new User();
$this->render('UrViewFileName',array('model'=>$model);

而不是发送你的禁用模型

$banned=new Banned();
$this->render('UrViewFileName',array('model'=>$banned);

我不确定,但我想这可能是你的问题

正如@Ninad在上面所说的$model,您正在渲染几种类型为Users而不是Banned的视图。用Banned替换Users的所有实例来纠正这个问题。admin方法应该是

public function actionAdmin()
{
    $model=new Banned('search'); 
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Banned']))
        $model->attributes=$_GET['Banned'];
                                                // Users
    $this->render('admin',array(
        'model'=>$model,
    ));
}