0

In a Yii2 advance application, I am trying to allow users to the login page and about page only.

So i put in the /common/config/web.php configuration file the following rule

    'as beforeRequest' => [  //if guest user access site so, redirect to login page.
    'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'actions' => ['login', 'error', 'request-password-reset', 'about'],
            'allow' => true,
        ],
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
], 

The login, error and request-password-reset it is working fine but not the about page.

I also tried '/page/about' and 'page/about' but i had no luck.

Any ideas how to fix or troubleshoot this.

Thanks

open-ecommerce.org
  • 1,763
  • 1
  • 25
  • 41

2 Answers2

0

It should be 'view' instead of 'about'

I am sorry I found the answer the rule it is fine but i am using a page component and the PageController it is getting the view from the admin (database) in the following controller

class PageController extends Controller{
public function actionView($slug)
{
    $model = Page::find()->where(['slug'=>$slug, 'status'=>Page::STATUS_PUBLISHED])->one();
    if (!$model) {
        throw new NotFoundHttpException(Yii::t('frontend', 'Page not found'));
    }

    $viewFile = $model->view ?: 'view';
    return $this->render($viewFile, ['model'=>$model]);
}}

So in the rule I need to use 'view' for all the views rendered by the PageController

open-ecommerce.org
  • 1,763
  • 1
  • 25
  • 41
0

try this code

'as beforeRequest' => [  //if guest user access site so, redirect to login page.


'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'actions' => ['login', 'error', 'request-password-reset', 'about'],
            'roles' => ['@']
            'allow' => true,
        ],
        [
            'allow' => true,
            'roles' => ['?'],
        ],
    ],
],

i hope it will work for you

Dani
  • 905
  • 7
  • 14