Call Component With Parameter

hi every body

i trying to call component in GridView so i have to call it inline like this




$this->beginWidget('bootstrap.widgets.BsGridView', array(

            'id'=>'users-grid',

            'dataProvider' => $model->search(),

            'filter' => $model,

            'filterCssClass' => 'filters',

            'type' => BsHtml::GRID_TYPE_STRIPED,

            'columns' => array(

array(

                    'name'=>'realScore',

                    'value'=>'Yii::app()->UserScore($data->id)->loadScore()',

                )


        ));




in main.php i have this




	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

            'loginUrl'=>array('admin/login'),

            'class' => 'WebUser',

        ),


        'UserScore'=>array(

            'class'=>'application.components.UserScore',

        ),

)



and my component is in components folder. but i’m getting error




CWebApplication and its behaviors do not have a method or closure named "UserScore".



and when i call the component this way




Yii::app()->UserScore->loadScore();



the error is




Missing argument 1 for UserScore::init(), called in D:\xampp\htdocs\faracharge\framework\base\CModule.php on line 387 and defined



plase help ;)

can you paste a dump of UserScore.php file

here is what I did just to show you

my component




<?php


class UserScore extends CApplicationComponent {


	public function score()

	{

		return 100;

	}


}

my config/main.php


// application components

	'components'=>array(


		'UserScore'=>array(

			'class'=>'application.components.UserScore',

		),

....



call to the component


<?= Yii::app()->UserScore->score() ?>

// prints 100

here see for youself

here is example of UserScore





class UserScore extends CComponent

{

    private $score = 0;


    public function __construct($score)

    {

        $this->score = $score;

    }


    public function init($score)

    {

        $this->score = $score;

    }




    public function loadScore()

    {

        return $this->score;

    }

}



the problem is that when i try to construct class like below it returns error :


CWebApplication and its behaviors do not have a method or closure named "UserScore".




$this->beginWidget('bootstrap.widgets.CGridView', array(

            'id'=>'users-grid',

            'dataProvider' => $model->search(),

            'filter' => $model,

            'columns' => array(

array(

                    'name'=>'realScore',

                    'value'=>'Yii::app()->UserScore(10)->loadScore', //try to construct with value 10 for score

                )


        ));



  1. UserScore must be a CApplicationComponent.



class UserScore extends CApplicationComponent

{

  private $score = 0;


  public function setScore($score) 

  {

    $this->score = $score;

    return $this;  //important for using 'chained'


  }






  1. You have to install the application component in config/main.php



'components' =>array(

  ...

  'userScore'=>'UserScore',


   //or if you want to assign more options like a default score


  'userScore'=>array(

      'class'=>'UserScore', 

      'score'=>20,

       ...  

  )

  ...

)










$this->beginWidget('bootstrap.widgets.CGridView', array(

       ...

            'columns' => array(

array(

                    'name'=>'realScore',

                    'value'=>'Yii::app()->userScore->setScore(10)->loadScore()',

                )


        ));




yes it’s working :lol:




return $this;



it’s realy important

thanks Joblo :D