0

I'm currently writing an app as a plugin to wordpress and hence I need some unconventional solutions..

Currently my problem is that when Yii gets an error it throws the error at its error handler and then it exits PHP execution. Shouldn't there be a way to sandbox Yii to make it only quit it's own execution and let the external code continue?

In my case most of the page goes blank and none of the wordpress theme gets loaded.. So it becomes pretty hacky to design good error pages. (When everything goes ok, wordpress loads in Yii in the_content(); and it blends in with the wordpress theme.)

Any ideas?

3 Answers 3

2

You have two options:

  1. You have to do try catch every single location where is possible to get exception
  2. Handling the error, then once exception came up, it would redirect all your app to a desired beauty error page (with Wordpress theme)

Yii allows using a controller action to handle the error display work. To do so, we should configure the error handler in the application configuration as follows:

return array(
    ......
    'components'=>array(
        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
    ),
);

In the above, we configure the CErrorHandler::errorAction property to be the route site/error which refers to the error action in SiteController. We may use a different route if needed.

public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
        $this->render('error', $error);
}

You has ability to customize the route as well as the view's theme when you are working with WordPress

Yii handling error details

Sign up to request clarification or add additional context in comments.

Comments

1

You'll probably want to end up overriding the default Yii error / exception handlers to put in custom ones. If you take a look at my article about integrating Yii/Wordpress, there is an example of an expection handler override. Adapt that to your use and you should be fine.

Adjusting the site/error handler is not going to end up working well for you, as that tends to throw a 400 error header that you can't stop even if you render a different page. Ran into that the hard way while integrating Yii and Wordpress, as I tried that first.

Let us know if you get it going, would love to have better example code for melding Yii on as a plugin.

2 Comments

Thank you! This is all I ever wanted!
@AlexanderKuzmin Glad to hear it ;-)
0

End Yii, with proper cleanup and without exiting the request. As shown on http://www.yiiframework.com/doc/api/1.1/CApplication#end-detail, it is done like so.

Yii::app()->end(0, false);

3 Comments

But doesn't that kill the PHP execution too?
@AlexanderKuzmin, how about with CApplication::end(0, false);? See yiiframework.com/doc/api/1.1/CApplication#end-detail for details.
@kekkis The problem is hooking the error handler in some way, because the error handler automatically calls Yii::app()->end() so I have to prevent that in some way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.