Handling Requests
Handling Requests in Yii 2
Content of the «Handling Requests» section:
- Request Handling Overview
- Bootstrapping
- Routing and URL Creation
- Requests
- Responses
- Sessions and Cookies
- Handling Errors
- Logging
Articles section «Handling Requests»:
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
Logging
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:
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
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.
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
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.
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
Responses
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.
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
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.
- Details
- Parent Category: The Definitive Guide to Yii 2.0
- Category: Handling Requests
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.
Page 1 of 2