[英]How to access only one specific page YII2 (Advanced Template)?
我在我的項目中為用戶提供了一種特殊情況。 如果用戶的訂閱已過期(我在login action
對此進行了檢查),他將被重定向到其profile
以編輯一些選擇。 如何阻止他訪問除他的profile
以外的任何頁面。 這是我login action
代碼
if($subPaymentType == 'free'){
$subHours = $data[0]['sub_hours'];
$minutes = $subHours * 60 * 60;
$start_time = date('d-m-Y H:i:s', $startDate);
$endDate = $minutes + strtotime($start_time);
$endDate = date('d-m-Y H:i:s', $endDate);
if(strtotime(date('d-m-Y:')) < strtotime($endDate)){
$model->login();
}else{
$model->login();
//User can access this only page only
return $this->redirect(['user/view/?id='.Yii::$app->user->id]);
}
}
您可以在控制器中使用use AccessControl
yii\filters\AccessControl;
class YourSiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login','profile'],
'allow' => true,
'roles' => ['*'],
],
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
有關更多信息,請參閱本指南http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html
並在相關的actionView中
public function actionView($id)
{
if ($id != Yii::$app->user->id){
// not allowed ... perform the action you need in this case
} else {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.