How To Call Controller/action In Console Command

I had create an action in controller and wish to reuse it by calling it in console command. my action in controller work fine in web but can’t call in command console. Please help.

class MyExampleController extends Controller

{

[indent]…

public function actionShowMe()

{

[indent]echo "testing";[/indent]}

…[/indent]

}


class GoCommand extends CConsoleCommand

{

[indent]public function run($args)

{

[indent]echo $this->renderInternal(‘myExample/showMe’);[/indent]}[/indent]}

I suggest you another way. For example you can create your own component:


class YourComponent

{

  public static function yourMethod()

  {

    echo "hello!";

  }

}

And call it from each methods you need:


class MyExampleController extends Controller

{

  public function actionShowMe()

  {

    YourComponent::yourMethod();

  }

}


class GoCommand extends CConsoleCommand

{

  public function run($args)

  {

    YourComponent::yourMethod();

  }

} 

Thanks for the suggestion. It help but in my case I use mpdf extension and I need to renderPartial for my dataProvider. How can I do it?





        public static function send()

        {

                  ......

                  $officer = new CArrayDataProvider($tmpOfficer, array(

                                    'pagination'=>array(

                                        'pageSize'=>count($tmpOfficer),

                                    ),

                  ));


                  $mPDF1 = Yii::app()->ePdf->mpdf();


                  $mPDF1 = Yii::app()->ePdf->mpdf('en-x','A4-L');

                  $stylesheet = file_get_contents(Yii::getPathOfAlias('webroot') . '\..\css\screen.css');

                  $mPDF1->WriteHTML($stylesheet, 1);

                  $mPDF1->WriteHTML($this->renderPartial('blastEmail2',array(

                            'dataProvider'=>$officer,

                            'title'=>"<b>" . $alarm->name . "</b><br/>" . $alarm->alert_day,

                            'email'=>$to,

                  ), true));

                  ...............

        }



I can’t use $this->renderPartial. How to do it?

I had do solve a similar problem when i wrote my pdfable extension. You can look at the PpdfFile class for some inspiration. [size=2]Maybe you even want to use my extension instead. It’s based on wkhtmltopdf and comes with [/size]command line support[size=2].[/size]

Try this :




public function run($args)

{

   Yii::import('application.modules.moduleName.controllers.DefaultController');

   $controller_instance = new DefaultController("Default"); 

   $controller_instance->actionIndex();

}




Here first i import controller called DefaultController.

then create instance of this controller and then call actionIndex.