Handling Requests

Yii 2 Handling Requests

Handling Requests in Yii 2

Content of the «Handling Requests» section:

Articles section «Handling Requests»:

Yii logging framework


Yii provides a powerful logging framework that is highly customizable and extensible. Using this framework, you can easily log various types of messages, filter them, and gather them at different targets, such as files, databases, emails.

Using the Yii logging framework involves the following steps:

Yii Handling Errors

Handling Errors

Yii includes a built-in [[yii\web\ErrorHandler|error handler]] which makes error handling a much more pleasant experience than before. In particular, the Yii error handler does the following to improve error handling:

  • All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.
  • Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines in debug mode.
  • Supports using a dedicated controller action to display errors.
  • Supports different error response formats.

Sessions and Cookies in Yii 2

Sessions and Cookies

Sessions and cookies allow data to be persisted across multiple user requests. In plain PHP you may access them through the global variables $_SESSION and $_COOKIE, respectively. Yii encapsulates sessions and cookies as objects and thus allows you to access them in an object-oriented fashion with additional useful enhancements.

Responses Yii 2


When an application finishes handling a request, it generates a [[yii\web\Response|response]] object and sends it to the end user. The response object contains information such as the HTTP status code, HTTP headers and body. The ultimate goal of Web application development is essentially to build such response objects upon various requests.

Yii 2 Requests


Requests made to an application are represented in terms of [[yii\web\Request]] objects which provide information such as request parameters, HTTP headers, cookies, etc. For a given request, you can get access to the corresponding request object via the request application component which is an instance of [[yii\web\Request]], by default. In this section, we will describe how you can make use of this component in your applications.

Routing and URL Creation in Yii 2

Routing and URL Creation

When a Yii application starts processing a requested URL, the first step it takes is to parse the URL into a route. The route is then used to instantiate the corresponding controller action to handle the request. This whole process is called routing.

The reverse process of routing is called URL creation, which creates a URL from a given route and the associated query parameters. When the created URL is later requested, the routing process can resolve it back into the original route and query parameters.