Error Handler for Modules

Hi,

My default error Handler is in site/error but I want a different error handler for an admin module. Is there a way to configure this?

You can try in admin module to do smth like:




	public function beforeControllerAction($controller, $action)

	{

             Yii::app()->errorHandler->errorAction='admin/default/error';

	}



Thanks for the suggestion but it’s not working. I have this in my AdminModule class




public function beforeControllerAction($controller, $action)

	{

		if(parent::beforeControllerAction($controller, $action))

		{

			Yii::app()->errorHandler->errorAction='admin/default/error';


			return true;

		}

		else

			return false;

	}



When requesting an invalid url inside of the admin module, the default site/error handler is invoked.

[font="arial, sans-serif"][size="6"]

[font="arial, sans-serif"][size=2]Hi[/size][/font][size=2]

[/size][font="arial, sans-serif"][size=2][/size][/font][size=2]I have the same question

[/size][/size][/font][size=2][/size][font="arial, sans-serif"][size=2]any help?[/size][/font]

I think that there is no solution.

If Yii cannot resolve the url because is invalid, it cannot eather know if is an invalid url of some module, is just invalid.

If you have an error in the controller or in the view, the correct even handler will be used.

one solution with a example

the code of module Drogueria

protected/modules/drogueria/DrogueriaModule.php





class DrogueriaModule extends CWebModule

{

	public function init()

	{

		$this->setImport(array(

			'drogueria.models.*',

			'drogueria.components.*',

		));


   	Yii::app()->setComponents(array(

            'errorHandler'=>array(

            'errorAction'=>'drogueria/default/error',

        ),


));




	}

.....



protected/modules/drogueria/controllers/DefaultController.php





class DefaultController extends Controller

{

    

    public function actionIndex()

	{

	$this->layout='main';

        $this->render('index');

	}


	public function actionError()

	{

    	if($error=Yii::app()->errorHandler->error)

    	{

    		if(Yii::app()->request->isAjaxRequest)

    			echo $error['message'];

    		else {

            	$this->layout='main';

                    $this->render('error', $error);

                }

                    

    	}

            

	}


}



protected/modules/drogueria/views/default/error.php





<h2>Error <?php echo $code; ?></h2>


<div class="error">

<?php echo CHtml::encode($message); ?>

</div>



Dude that works for me , Thanks heaps

Thanks a lot… I had the same problem and it works for me too.

Nice! Thank you OP for the thread and Horacio for the perfect solution.

Edit:

For me, this solution caused the render to show the content (the error message) without a containing div. I had to remove the following line in DefaultController::actionError()


$this->layout='main';

because in my system, the default layout is defined in the Controller class (see below) with a single slash (’/’) which causes the render to choose the correct layout for the module by itself.

Default layout defined in Controller class:


public $layout='/layouts/column1';

Column1.php:


<?php $this->beginContent('/layouts/main'); ?>

<div id="content">

	<?php echo $content; ?>

</div><!-- content -->

<?php $this->endContent(); ?>

More about the single vs double slash:

http://www.yiiframework.com/doc/api/1.1/CController#getLayoutFile-detail

hi guys… i met one problem. i used the solution with great success…until i added in cache for yii. using memcache.




'cache'=>array(

            'class'=>'system.caching.CMemCache',

            'servers'=>array(

                array('host'=>'localhost', 'port'=>11211, 'weight'=>100),

            ),

        ),



afterwhich, individual modules error handlers do not work anymore. for some reason, everything is being directed to error handler of user module. if it is helpful, user module only has one controller. defaultcontroller.

just for verification purpose, i turned off caching and it works again…

anyone has any insight on this??