D.R. D.R. - 1 month ago 18
PHP Question

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.

Answer

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

Comments