Force Login To Create Comment


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.


        // Comments controller

        public function accessRules()


		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions




			array('allow', // allow authenticated user to perform 'create' and 'update' actions




			array('allow', // allow admin user to perform 'admin' and 'delete' actions




			array('deny',  // deny all users





why couldn’t move comment creation logic to post controller?

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?

Has anybody else run into this issue?

I want the users of my website to login first and then enter my application.

I tried the following code in my

config/main.php, on the first array

‘behaviors’ => array(

'onBeginRequest' => array(

    'class' => 'application.components.RequireLogin'



I also created a page called requirelogin.php

There i created this class in components

class RequireLogin extends CBehavior


public function attach($owner)


$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));


	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

Kindly help

Please take a look at the official Blog example of Yii :)

I saw the blog example, but it allows me to navigate the site. I don’t want that feature…

I want the users to login first and then navigate the site