Below are code snippets I am using. It works great but I want to allow access to one more page that is located under /site/pages/news.php. I attempted to add it to the array several different ways but I am stillb being forced to login on this page.
***config main.php
‘behaviors’ => array(
'onBeginRequest' => array(
'class' => 'application.components.RequireLogin'
)
),
***requirelogin.php
class RequireLogin extends CBehavior
{
public function attach($owner)
{
$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
}
public function handleBeginRequest($event)
{
$app = Yii::app();
$user = $app->user;
$request = trim($app->urlManager->parseUrl($app->request), '/');
$login = trim($user->loginUrl[0], '/');
// Restrict guests to public pages.
$allowed = array($login,'site/index','site/contact');
if ($user->isGuest && !in_array($request, $allowed))
$user->loginRequired();
// Prevent logged in users from viewing the login page.
$request = substr($request, 0, strlen($login));
if (!$user->isGuest && $request == $login)
{
$url = $app->createUrl($app->homeUrl[0]);
$app->request->redirect($url);
}
}
}