Php Yii:无法加载url页面";错误404无法解析请求;用户/userpage"&引用;

Php Yii:无法加载url页面";错误404无法解析请求;用户/userpage"&引用;,php,yii,Php,Yii,我是一个易受影响的人,我试图根据用户id从用户表中获取用户数据。为此,我在视图中创建了一个userpage.php文件,并在user controller中编写了一个名为userpage的函数,以从表中获取用户数据。完成所有这些之后,当我编写url“localhost/projectname/user/userpage?id=“anyid”时,它会给我一个错误 Error 404 Unable to resolve the request "user/userpage". 这是我的UserCo

我是一个易受影响的人,我试图根据用户id从用户表中获取用户数据。为此,我在视图中创建了一个
userpage.php
文件,并在
user controller
中编写了一个名为
userpage
的函数,以从表中获取用户数据。完成所有这些之后,当我编写url
“localhost/projectname/user/userpage?id=“anyid”
时,它会给我一个错误

Error 404
Unable to resolve the request "user/userpage".
这是我的
UserController

<?php

class UserController extends RController
{
    /**
    * @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/admin';

    /**
    * @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
                    'rights',
        );
    }

    /**
    * 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),
        ));
    }
        public function actionUserpage($id)
        {
            $this->layout='main';
        $this->render('userpage',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 User;

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

        if(isset($_POST['User']))
        {
                    $rnd = rand(0,9999);  // generate random number between 0-9999
            $model->attributes=$_POST['User'];
                        $uploadedFile=CUploadedFile::getInstance($model,'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            $model->image = $fileName;
            if($model->save())
                            {
                $uploadedFile->saveAs(Yii::app()->basePath.'/'.$fileName);  // image will uplode to rootDirectory/event/
                $this->redirect(array('admin'));
            }
                $this->redirect(array('view','id'=>$model->id));
        }

        $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['User']))
        {
                    $_POST['User']['image'] = $model->image;
            $model->attributes=$_POST['User'];
                        $uploadedFile=CUploadedFile::getInstance($model,'image');
            if($model->save())
                             {
                if(!empty($uploadedFile))  // check if uploaded file is set or not
                {
                 $uploadedFile->saveAs(Yii::app()->basePath.'/'.$model->image);
                }
                $this->redirect(array('admin'));
            }
                $this->redirect(array('view','id'=>$model->id));
        }

        $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('User');
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

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

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

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

您还需要在
public function accessRules()
函数中注册
userpage
操作,否则无法从url访问此操作

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','userpage'),
                '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('*'),
            ),
        );
    }

请注意,上述更改只允许用户使用
userpage
操作,如果您希望它是公共的,请将其添加到
'users'=>array('*'),
array

检查是否有一个名为

 userpage.php 
在用户的视图目录中 (用户的相关视图目录取决于您使用的用户组件。)

检查应用程序的名称空间或config/main.php以查找正确的目录


还要检查您是否在用户模型的accessRules中正确分配了userpage

并且绝对确保您使用的是有效的$id值。因为如果在db中找不到id的值,则应引发此类消息

然后尝试添加prettyUrl=true,否则无法使用符号

     'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'enablePrettyUrl' => true,   // Disable r= routes
        'rules'=>array(
    //'<controller:\w+>'=>'<controller>/list',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            '<controller:\w+>/<id:\d+>/<title>'=>'<controller>/view',
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        ),
    ),
'urlManager'=>数组(
“urlFormat'=>“路径”,
'showScriptName'=>false,
'enablePrettyUrl'=>true,//禁用r=routes
'规则'=>数组(
//''=>''/list',
'/'=>'/',
“//”=>“/视图”,
“/”=>“/视图”,
),
),

如何操作,请更新上述代码。谢谢这还没有解决,仍然会出现同样的错误,我在访问规则中复制了整个内容。请查看是否存在任何其他问题。当您访问url时,是否登录?是否?是,我已登录。。!!我认为url中的项目文件夹名称有问题,请尝试更改config/routes.php中的设置,如$config['base_url']=”;在您的用户模型或Usercontroller的accessRules中正确分配了userpage…?对不起,在controller中,accessRules在controller中,请检查条目“userpage”是否位于正确的数组中。对于测试,我建议您将条目插入到无限制条目(*)中,这样您就不用担心身份验证了..我已经尝试了所有这些方法,但我不知道为什么什么都没有发生,我已经用我的主..php文件更新了问题,请查看一下。您使用RWebUser组件,相关视图放在哪里?也给我看一下控制器的名称。只是一个简单的问题。当您不调用localhost/projectname/user/userpage?id=“anyid”而调用localhost/projectname/user/view?id=“anyid”…时会发生什么情况。。。行吗?
 userpage.php 
     'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'enablePrettyUrl' => true,   // Disable r= routes
        'rules'=>array(
    //'<controller:\w+>'=>'<controller>/list',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            '<controller:\w+>/<id:\d+>/<title>'=>'<controller>/view',
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        ),
    ),