Yii2。 dektrium /用户。自定义控制器操作重定向到登录页面

时间:2016-10-16 16:01:05

标签: php yii2 url-routing yii2-basic-app yii2-user

问题

每个自定义操作都会重定向回登录页面。

我的代码

我已从dektrium\user\controllers\RegistrationController

扩展了我的自定义控制器

我的web.php

 'urlManager'   => [
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'rules'           => [

            ],
        ],

...

'modules'    => [
        'user' => [
            'class'         => 'dektrium\user\Module',
            'controllerMap' => [
                'registration' => 'app\controllers\user\RegistrationController'
            ],
        ],
    ],

自定义控制器

namespace app\controllers\user;

use dektrium\user\controllers\RegistrationController as BaseAdminController;

class RegistrationController extends BaseAdminController
{
    public function actionPlan()
    {
        echo 'Test';
    }
}

Overrode方法运行良好,但每个自定义操作(site.com/user/registration/plan)都会重定向回登录页面。

2 个答案:

答案 0 :(得分:2)

如果您想更改访问控制规则,您应该正确更改,例如:在您的站点控制器中将计划添加到可访问的规则而无需验证

class SiteController extends Controller
{
/**
 * @inheritdoc
 */
   public function behaviors()
   {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['login','plan',  'error'],
                    'allow' => true,
                ],
                [
                    'actions' => ['logout', 'index'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

了解更多http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html

答案 1 :(得分:0)

另一个实现

class SiteController extends Controller
{
  /**
   * @inheritdoc
   */
   public function behaviors()
   {
    $behaviors = [
        'access' => [
            'rules' => [
                [
                    'actions' => ['login', 'plan', 'error'],
                    'allow'   => true,
                ],
            ],
        ],
    ];

    return ArrayHelper::merge($behaviors, parent::behaviors());
    }
}