处理cjuiautocomplete显示异常

我尝试了来自te的源代码

我用于CJUIAutoComplete的源代码

没有得到错误的地方 显示为未授权的错误,请帮助我,我是Yii框架和调试的新手。

这里是我的错误日志

2012/05/25 06:07:28 [error] [exception.CHttpException.403] exception 'CHttpException' with message 'You are not authorized to perform this action.' in C:\wamp\yii\framework\web\auth\CAccessControlFilter.php:157 

Stack trace:
#0 C:\wamp\yii\framework\web\auth\CAccessControlFilter.php(120) CAccessControlFilter->accessDenied(Object(CWebUser), 'You are not aut...')
#1 C:\wamp\yii\framework\web\filters\CFilter.php(39) CAccessControlFilter->preFilter(Object(CFilterChain))

#2 C:\wamp\yii\framework\web\CController.php(1146): CFilter->filter(Object(CFilterChain))
#3 C:\wamp\yii\framework\web\filters\CInlineFilter.php(59) CController->filterAccessControl(Object(CFilterChain))


#4 C:\wamp\yii\framework\web\filters\CFilterChain.php(131) CInlineFilter->filter(Object(CFilterChain))

#5 C:\wamp\yii\framework\web\CController.php(292): CFilterChain->run()

#6 C:\wamp\yii\framework\web\CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array)

#7 C:\wamp\yii\framework\web\CWebApplication.php(276): CController->run('autocompleteTes...')

#8 C:\wamp\yii\framework\web\CWebApplication.php(135): CWebApplication->runController('details/autocom...')

#9 C:\wamp\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()

#10 C:\wamp\www\myapp\index.php(13): CApplication->run()

#11 {main}
REQUEST_URI=/myapp/index.php?r=details/autocompleteTest&term=asss
HTTP_REFERER=http://localhost/myapp/index.php?r=details/create

控制器代码

<?php
class DetailsController 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/column2';

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl',//perform access control for CRUD operations
    );
}

    public function actionAutocompleteTest() {
$res =array();

if (isset($_GET['term'])) {
    $qtxt ="SELECT name FROM {{details}} WHERE name LIKE :username";
    $command =Yii::app()->db->createCommand($qtxt);
    $command->bindValue(":name", '%'.$_GET['term'].'%', PDO::PARAM_STR);
    $res =$command->queryColumn();
}

echo CJSON::encode($res);
Yii::app()->end();
}


/**
 * 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 all users to perform 'index' and 'view' actions
            'actions'=>array('index','view'),
            'users'=>array('*'),
        ),
        array('allow',//allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update'),
            'users'=>array('@'),
        ),
        array('allow',//allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','delete'),
            '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)
{
    $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 Details;

   //Uncomment the following line if AJAX validation is needed
   //$this->performAjaxValidation($model);

    if(isset($_POST['Details']))
    {
        $model->attributes=$_POST['Details'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id1));
    }

    $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['Details']))
    {
        $model->attributes=$_POST['Details'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id1));
    }

    $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)
{
    if(Yii::app()->request->isPostRequest)
    {
       //we only allow deletion via POST request
        $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'));
    }
    else
        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}

/**
 * Lists all models.
 */
public function actionIndex()
{
    $dataProvider=new CActiveDataProvider('Details');
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

/**
 * Manages all models.
 */
public function actionAdmin()
{
    $model=new Details('search');
    $model->unsetAttributes(); //clear any default values
    if(isset($_GET['Details']))
        $model->attributes=$_GET['Details'];

    $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 the ID of the model to be loaded
 */
public function loadModel($id)
{
    $model=Details::model()->findByPk($id);
    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}



/**
 * Performs the AJAX validation.
 * @param CModel the model to be validated
 */
protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='details-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}
}

表单代码

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'details-form',
    'enableAjaxValidation'=>false,
)); ?>

    
Fields with * are required.

<?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'id'); ?> <?php echo $form->textField($model,'id'); ?> <?php echo $form->error($model,'id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'name'); ?> <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array( 'name'=>'test1', 'value'=>'test21', 'source'=>$this->createUrl('details/autocompleteTest'), //additional JavaScript options for the autocomplete plugin 'options'=>array( 'showAnim'=>'fold', ), )); ?> <?php echo $form->error($model,'name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'phone'); ?> <?php echo $form->textField($model,'phone'); ?> <?php echo $form->error($model,'phone'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'email'); ?> <?php echo $form->textField($model,'email'); ?> <?php echo $form->error($model,'email'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->

谢谢 Devendar

0
额外
意见: 1

3 答案

你必须更新你的DetailController:

array('allow', 
     //allow authenticated user to perform 'create', 'update' and 'autocompleteTest' actions
      'actions'=>array('create','update', 'autocompleteTest'),
      'users'=>array('@'),
),
1
额外
@谢谢感谢您的答复 - 但仍显示相同的错误日志
额外 作者 Devendar,
我在accessrules中添加 - 但也在错误日志中的相同消息
额外 作者 Devendar,
我在actionAutocompleteTest()函数下的DetailConstroller中添加了上述代码 - 是吗?
额外 作者 Devendar,

控制器代码:

名称是模型的属性(数据库表)

public function actionAutoCompleteLookup() {
    $res =array();

    if (isset($_GET['term'])) {

        $qtxt ="SELECT name FROM customer WHERE name LIKE :name";
        $command =Yii::app()->db->createCommand($qtxt);
        $command->bindValue(":name", '%'.$_GET['term'].'%', PDO::PARAM_STR);
        $res =$command->queryColumn();
    }

    echo CJSON::encode($res);
    Yii::app()->end();
}

查看代码

创建URL - 控制器名称/ Actin(用控制器写的函数名称)

        <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'name'=>'test2',
    'source'=>$this->createUrl('Customer/AutoCompleteLookup'),

             'options'=>array(
             'showAnim'=>'fold',
              ), 
));
    ?>

感谢Stackoverflow

0
额外

试试这对你来说非常简单..

public function actionAutoCompleteLookup()
        {
           if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
           {

              $name = $_GET['q']; 

               $qtxt ="SELECT name FROM details WHERE name LIKE '%".$name."%'";
               $command =Yii::app()->db->createCommand($qtxt);

               $userArray =$command->queryColumn();

              $returnVal = '';
              foreach($userArray as $userAccount)
              {
                 $returnVal .= $userAccount->getAttribute('first_name').'|'
                                             .$userAccount->getAttribute('user_id')."\n";
              }
              echo $returnVal;
           }
        }

在这样的视图代码中

<?php $this->widget('CAutoComplete',
          array(
                         //name of the html field that will be generated
             'name'=>'name', 
                       //replace controller/action with real ids
             'url'=>array('detail/AutoCompleteLookup'), 
             'max'=>10, //specifies the max number of items to display

                         //specifies the number of chars that must be entered 
                         //before autocomplete initiates a lookup

             ));
    ?>

它的工作很好...

0
额外
#3 C:\ wamp \ yii \ framework \ web \ filters \ CInlineFilter.php(59):CController-> filterAccessControl(Object(CFilterChain))#4 C:\ wamp \ yii \ framework \ web \ filters \ CFilterChain.php (131)CInlineFilter-> filter(Object(CFilterChain))#5 C:\ wamp \ yii \ framework \ web \ CController.php(292):CFilterChain-> run()#6 C:\ wamp \ yii \ framework \ web \ CController.php(266):CController-> runActionWithFilters(Object(CInlineAction),Array)#7 C:\ wamp \ yii \ framework \ web \ CWebApplication.php(276):CController-> run('AutoCompleteLoo。 ..“)
额外 作者 Devendar,
谢谢你的帮助 - 最后我明白了
额外 作者 Devendar,
请给我你的ID - 我会发送总代码
额外 作者 Devendar,
这是你的ID在fb:owais.iqbal.357
额外 作者 Devendar,
请登录我的系统一次,否则我可以发送总应用程序代码
额外 作者 Devendar,
teamviewr - 给我你个人的身份证,否则我的ID是--devendar.gona在gmail.com
额外 作者 Devendar,
朋友如果你有空 - 请尝试登录我的系统 - 我从2天来到这里
额外 作者 Devendar,
那么同样的错误的朋友
额外 作者 Devendar,
#8 C:\ wamp \ yii \ framework \ web \ CWebApplication.php(135):CWebApplication-> runController('Details/AutoCom ...')#9 C:\ wamp \ yii \ framework \ base \ CApplication.php (162):CWebApplication-> processRequest()#10 C:\ wamp \ www \ myapp \ index.php(13):CApplication-> run()#11 {main} REQUEST_URI =/myapp/index.php?r =详情/ AutoCompleteLookup&q =&zwnj; de&limit = 10&timestam&zwnj; p = 1337932062468 HTTP_REFERER = localhost/myapp /index.php?r=details/create
额外 作者 Devendar,
2012/05/25 07:47:42 [error] [exception.CHttpException.403]异常'CHttpException'带有消息'您无权执行此操作。'在C:\ wamp \ yii \ framework \ web \ auth \ CAccessControlFilter.php:157 Stack trace:#0 C:\ wamp \ yii \ framework \ web \ auth \ CAccessControlFilter.php(120)&zwnj;:CAccessControlFilter-> #1 C:\ wamp \ yii \ framework \ web \ filters \ CFilter.php(39):CAccessControlFilter-> preFilter(Object(CFilterChain))# 2 C:\ wamp \ yii \ framework \ web \ CController.php(1146):CFilter-> filter(Object(CFilterChain))
额外 作者 Devendar,
我正在使用Db名称 - 详细信息 - 控制器名称为DetailsController.php,并在_form.php文件中添加了视图代码。我db表属性是id1,id,名字,电话,电子邮件。
额外 作者 Devendar,
是我写的朋友
额外 作者 Devendar,
如果我更改为(控制器名称/操作) - 显示未经授权
额外 作者 Devendar,
#3 {main} REQUEST_URI =/myapp/index.php?r = admin/AutoCompleteLookup&q = de&zwnj;&limit = 10&timestamp =&zwnj; 1337928948219 HTTP_REFERER = localhost/myapp/index.php?r = details/create
额外 作者 Devendar,
我照原样复制 - 在错误日志2012/05/25 06:55:48 [error] [exception.CHttpException.404]中显示此错误,并显示'无法解析请求'admin/AutoCompleteLookup'异常'CHttpException'。 “在C:\ wamp \ yii \ framework \ web \ CWebApplication.php中:280 Stack trace:#0 C:\ wamp \ yii \ framework \ web \ CWebApplication.php(135):CWebApplication-> runController('admin/AutoCompl。 ..')#1 C:\ wamp \ yii \ framework \ base \ CApplication.php(162):CWebApplication-> processRequest()#2 C:\ wamp \ www \ myapp \ index.php(13):CApplication- > run()的
额外 作者 Devendar,
所以你现在得到的错误..复制错误
额外 作者 Owais Iqbal,
IN view ...在窗体外部使用这个小部件..然后检查...
额外 作者 Owais Iqbal,
你写这样..'url'=>数组(Details/AutoCompleteLookup),
额外 作者 Owais Iqbal,
我如何登录到你的系统freind?
额外 作者 Owais Iqbal,
在视图中给出错误的Url ...正确的Url像'url'=> array('Controller name/AutoCompleteLookup');
额外 作者 Owais Iqbal,
对不起freind ..我检查了我的代码..它正常运行..所以我认为你有你的配置文件或其他地方的问题..
额外 作者 Owais Iqbal,
哈哈是不是我的id :)
额外 作者 Owais Iqbal,