I have a Post with many comments associated with it. I have a Comment form at the bottom of each Post. Business rules stipulate that any user may view posts but only authenticated users can create posts and comments. How/where do I declare the rules to force users to login before posting comments.
I would assume that the Comments controller is the best place but it is not working. I have set my access rules to only allow authenticated users to be able to create comments. See code snippet. This works when trying to create comments through the comments controller but not when creating comments through the Post controller - as in my case.
See attached file for screenshot.
Thanks.
// Comments controller
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
I could, but wouldn’t that defeat the purpose of logic/controller separation? I.e. my CreateComment method would be in the PostController and my view, update, delete methods in the Comment Controller.
I suppose I could route the request to the CommentController, find the associated Post by PK, and load the Post view again from the Comment Controller. I don’t think there are any MVC restrictions on calling the Post view from the Comment Controller. Are there?
public function handleBeginRequest($event)
{
if (Yii::app()->user->isGuest && (!isset($_GET['r']) || $_GET['r'] != 'site/index'))
{
Yii::app()->user->loginRequired(); // Redirection for login
}
}
}
It is not able to redirect to login page, it get’s stuck