use of componet

I am using session .If user logout and he write url for profile page profile page should not open.it should directly redirect to login page.I did it .But i write the code in every controller.


<?php

class PendingListController extends Controller

{

    public function actionIndex()

    {

        $login_id       = Yii::app()->session['login_id'];

        $type           = yii::app()->session['type'];

        if($login_id && $type=='A')

        {

            $messagemodel = new Student('search');

            $messagemodel->unsetAttributes();

            if(isset($_GET['Student']))

            {

                $messagemodel->attributes=$_GET['Student'];

            }

            $this->render('index',  array('messagemodel'=>$messagemodel));      

         }

         else 

        {  

            $this->redirect(array('/site/login'));

        }  

         

    }

} // End Class

?>

this type of code i write in each controller .But i think this not a reuse ability of code.so i think i should create a component n write the code there.But i dont know how to do it can any one help me .with example .

What U can do is to place this code in

Controller class in to beforeAction().

Something like :

beforeAction($action)

{

$login_id = Yii::app()->session[‘login_id’];

$type = yii::app()->session[‘type’];

if(!$login_id || $type != ‘A’)

    &#036;this-&gt;redirect(array('/site/login'));

return parent::beforeAction($action);

}

since all controller classes are extended from it U can maintain and update this functionality and logic on one place and it should do your Job. Don’t think that there is a need for creating component just for this functionality

check this also : http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail

You should also check this, cause maybe it will save U time writing session, since this is Yii native functionlity

http://www.yiiframework.com/doc/api/1.1/CWebUser#isGuest-detail

I get it but if i want to write in component then what should i do .

what thing i need to write in component.what is use of component

Check this

http://www.yiiframework.com/doc/api/1.1/CApplicationComponent