Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Yii中动态设置默认动作_Yii_Action_Default - Fatal编程技术网

如何在Yii中动态设置默认动作

如何在Yii中动态设置默认动作,yii,action,default,Yii,Action,Default,我想更改控制器的默认操作,这取决于登录的用户。 例如,我的站点中有两个用户:publisher和author,我想在publisher登录时将publisher操作设置为默认操作,而author也一样 我该怎么办?我什么时候可以检查我的角色并设置它们的相关操作?我想您可以在用户表中保存第一个用户页面。当用户通过身份验证时,您可以从数据库加载此页面。你在哪里可以做到这一点?我认为最好的地方是UserIdentity类。之后,您可以在SiteController::actionLogin中获得该值

我想更改控制器的默认操作,这取决于登录的用户。 例如,我的站点中有两个用户:publisher和author,我想在publisher登录时将publisher操作设置为默认操作,而author也一样


我该怎么办?我什么时候可以检查我的角色并设置它们的相关操作?

我想您可以在用户表中保存第一个用户页面。当用户通过身份验证时,您可以从数据库加载此页面。你在哪里可以做到这一点?我认为最好的地方是UserIdentity类。之后,您可以在SiteController::actionLogin中获得该值

您可以获取或设置首页值:

            if (null === $user->first_page) {
                $firstPage = 'site/index';
            } else {
                $firstPage = $user->first_page;
            }
这是一个完整的类:

class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {
        $user = User::model()->findByAttributes(array('username' => $this->username));
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($user->password !== $user->encrypt($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;

            if (null === $user->first_page) {
                $firstPage = 'site/index';
            } else {
                $firstPage = $user->first_page;
            }

            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }

}



/**
 * Displays the login page
 */
public function actionLogin()
{
    $model = new LoginForm;

    // if it is ajax validation request
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if (isset($_POST['LoginForm'])) {
        $model->attributes = $_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if ($model->validate() && $model->login())
            $this->redirect(Yii::app()->user->first_page);
    }

    // display the login form
    $this->render('login', array('model' => $model));
}

此外,您只能在该文件中编写正确的代码。在SiteController文件中。

我认为您可以保存用户表中的第一个用户页。当用户通过身份验证时,您可以从数据库加载此页面。你在哪里可以做到这一点?我认为最好的地方是UserIdentity类。之后,您可以在SiteController::actionLogin中获得该值

您可以获取或设置首页值:

            if (null === $user->first_page) {
                $firstPage = 'site/index';
            } else {
                $firstPage = $user->first_page;
            }
这是一个完整的类:

class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {
        $user = User::model()->findByAttributes(array('username' => $this->username));
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($user->password !== $user->encrypt($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;

            if (null === $user->first_page) {
                $firstPage = 'site/index';
            } else {
                $firstPage = $user->first_page;
            }

            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }

}



/**
 * Displays the login page
 */
public function actionLogin()
{
    $model = new LoginForm;

    // if it is ajax validation request
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if (isset($_POST['LoginForm'])) {
        $model->attributes = $_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if ($model->validate() && $model->login())
            $this->redirect(Yii::app()->user->first_page);
    }

    // display the login form
    $this->render('login', array('model' => $model));
}

此外,您只能在该文件中编写正确的代码。在SiteController文件中。

您可以做的另一件更简单的事情是保持默认操作不变,但默认操作只是根据登录的用户类型调用一个附加操作函数。例如,您可以使用indexAction函数有条件地调用this->userAction或this->publisherAction,具体取决于对登录用户的检查。

另一个更简单的方法是保持默认操作不变,但默认操作只会根据登录的用户类型调用其他操作函数。例如,您可以使用indexAction函数有条件地调用this->userAction或this->publisherAction,具体取决于对登录用户的检查。

另一种方法是在控制器的。有点像这样:

<?php
class MyAwesomeController extends Controller{ // or extends CController depending on your code
    public function init(){
        parent::init(); // no need for this call if you don't have anything in your parent init() 

        if(array_key_exists('RolePublisher', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
            $this->defaultAction='publisher'; // name of your action

        else if (array_key_exists('RoleAuthor', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
            $this->defaultAction='author'; // name of your action
    }
    // ... rest of your code
}
?>
签出,查看返回的数组的格式是否为'role'=>CAuthItem对象,这就是为什么我要使用array\u key\u exists进行检查


如果您不知道,则操作名称将只是没有操作部分的名称,例如,如果您有公共函数actionPublisher{…},则操作名称应为:publisher。

另一种方法是在控制器中设置。有点像这样:

<?php
class MyAwesomeController extends Controller{ // or extends CController depending on your code
    public function init(){
        parent::init(); // no need for this call if you don't have anything in your parent init() 

        if(array_key_exists('RolePublisher', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
            $this->defaultAction='publisher'; // name of your action

        else if (array_key_exists('RoleAuthor', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
            $this->defaultAction='author'; // name of your action
    }
    // ... rest of your code
}
?>
签出,查看返回的数组的格式是否为'role'=>CAuthItem对象,这就是为什么我要使用array\u key\u exists进行检查


如果您不知道,操作名称将只是名称而不包含操作部分,例如,如果您有公共函数actionPublisher{…},则操作名称应为:publisher。

将用户重定向到其地址是一个好主意。但我不需要将他们的地址保存到DB,我可以为userIdentity中的每个角色分配地址。非常感谢。这是一个将用户重定向到其地址的好主意。但我不需要将他们的地址保存到DB,我可以为userIdentity中的每个角色分配地址。非常感谢。如果您有不同的角色含义,即如果未使用yii的rbac,则必须修改检查以使用您的角色含义。如果您有不同的角色含义,即如果未使用yii的rbac,则必须修改检查以使用您的角色含义。