简体   繁体   中英

Yii2. dektrium/user. Custom controller action redirects to the login page

Problem

Every custom action redirects back to the login page.

My code

I've extended my custom controller from the dektrium\\user\\controllers\\RegistrationController

My web.php

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

            ],
        ],

...

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

Custom controller

namespace app\controllers\user;

use dektrium\user\controllers\RegistrationController as BaseAdminController;

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

Overrode methods works good, but each custom action ( site.com/user/registration/plan ) redirects back to the login page.

If you want change the access control rules you should change properly eg: in your site controller add plan to the rules accessible without authenctication

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'],
            ],
        ],
    ];
}

see this for more http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

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

Another implementation

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM