20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Yii</strong> defines two exception classes: CException and CHttpException. The former is a<br />

generic exception class, while the latter represents an exception that should be displayed<br />

to end users. The latter also carries a statusCode property representing an HTTP status<br />

code. The class of an exception determines how it should be displayed, as we will explain<br />

next.<br />

Tip: Raising a CHttpException exception is a simple way of reporting errors caused by<br />

user misoperation. For example, if the user provides an invalid post ID in the URL, we<br />

can simply <strong>do</strong> the following to show a 404 error (page not found):<br />

// if post ID is invalid<br />

throw new CHttpException(404,'The specified post cannot be found.');<br />

Displaying Errors<br />

When an error is forwarded to the CErrorHandler application component, it chooses an<br />

appropriate view to display the error. If the error is meant to be displayed to end users,<br />

such as a CHttpException, it will use a view named errorXXX, where XXX stands for the<br />

HTTP status code (e.g. 400, 404, 500). If the error is an internal one and should only be<br />

displayed to developers, it will use a view named exception. In the latter case, complete<br />

call stack as well as the error line information will be displayed.<br />

Info: When the application runs in production mode, all errors including those internal<br />

ones will be displayed using view errorXXX. This is because the call stack of an error<br />

may contain sensitive information. In this case, developers should rely on the error logs<br />

to determine what is the real cause of an error.<br />

CErrorHandler searches for the view file corresponding to a view in the following order:<br />

• WebRoot/themes/ThemeName/views/system: this is the system view directory under<br />

the currently active theme.<br />

• WebRoot/protected/views/system: this is the default system view directory for an<br />

application.<br />

• yii/framework/views: this is the standard system view directory provided by the <strong>Yii</strong><br />

framework.<br />

Therefore, if we want to customize the error display, we can simply create error view files<br />

under the system view directory of our application or theme. Each view file is a normal<br />

PHP script consisting of mainly HTML code. For more details, please refer to the default<br />

view files under the framework's view directory.<br />

Handling Errors Using an Action<br />

Starting from version 1.0.6, <strong>Yii</strong> allows using a controller action to handle the error display<br />

work. To <strong>do</strong> so, we should configure the error handler in the application configuration as<br />

follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!