4. Tipología de los conflictos ambientales
4.2. Decisiones de las autoridades
4.2.2. Caso Samoré, pueblo indígena U’wa
124 Extensions
Each time when a Yii application handles a request, it undergoes a similar workflow.
1. A user makes a request to the entry script web/index.php .
2. The entry script loads the application configuration and creates an application instance to handle the request.
3. The application resolves the requested route with the help of the request application component.
4. The application creates a controller instance to handle the request.
5. The controller creates an action instance and performs the filters for the action.
6. If any filter fails, the action is cancelled.
7. If all filters pass, the action is executed.
8. The action loads a data model, possibly from a database.
9. The action renders a view, providing it with the data model.
10. The rendered result is returned to the response application component.
11. The response component sends the rendered result to the user's browser.
The following diagram shows how an application handles a request.
In this section, we will describe in detail how some of these steps work.
Overview
125 Overview
Bootstrapping refers to the process of preparing the environment before an application starts to resolve and process an incoming request. Bootstrapping is done in two places: the entry script and the application.
In the entry script, class autoloaders for different libraries are registered. This includes the Composer autoloader through its
autoload.php file and the Yii autoloader through its Yii class file. The entry script then loads the application configuration and creates an application instance.
In the constructor of the application, the following bootstrapping work is done:
1. [[yii\base\Application::preInit()|preInit()]] is called, which configures some high priority application properties, such as [[yii\base\Application::basePath|basePath]].
2. Register the [[yii\base\Application::errorHandler|error handler]].
3. Initialize application properties using the given application configuration.
4. [[yii\base\Application::init()|init()]] is called which in turn calls [[yii\base\Application::bootstrap()|bootstrap()]] to run bootstrapping components.
Include the extension manifest file vendor/yiisoft/extensions.php . Create and run bootstrap components declared by extensions.
Create and run application components and/or modules that are declared in the application's bootstrap property.
Because the bootstrapping work has to be done before handling every request, it is very important to keep this process light and optimize it as much as possible.
Try not to register too many bootstrapping components. A bootstrapping component is needed only if it wants to participate the whole life cycle of requesting handling. For example, if a module needs to register additional URL parsing rules, it should be listed in the bootstrap property so that the new URL rules can take effect before they are used to resolve requests.
In production mode, enable a bytecode cache, such as PHP OPcache or APC, to minimize the time needed for including and parsing PHP files.
Some large applications have very complex application configurations which are divided into many smaller configuration files. If this is the case, consider caching the whole configuration array and loading it directly from cache before creating the application instance in the entry script.
Bootstrapping
126 Bootstrapping
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.
The central piece responsible for routing and URL creation is the [[yii\web\UrlManager|URL manager]], which is registered as the urlManager application component. The [[yii\web\UrlManager|URL manager]] provides the
[[yii\web\UrlManager::parseRequest()|parseRequest()]] method to parse an incoming request into a route and the associated query parameters and the [[yii\web\UrlManager::createUrl()|createUrl()]] method to create a URL from a given route and its associated query parameters.
By configuring the urlManager component in the application configuration, you can let your application recognize arbitrary URL formats without modifying your existing application code. For example, you can use the following code to create a URL for the post/view action:
use yii\helpers\Url;
// Url::to() calls UrlManager::createUrl() to create a URL
$url = Url::to(['post/view', 'id' => 100]);
Depending on the urlManager configuration, the created URL may look like one of the following (or other format). And if the created URL is requested later, it will still be parsed back into the original route and query parameter value.
/index.php?r=post/view&id=100 /index.php/post/100
/posts/100
The [[yii\web\UrlManager|URL manager]] supports two URL formats: the default URL format and the pretty URL format.
The default URL format uses a query parameter named r to represent the route and normal query parameters to represent the query parameters associated with the route. For example, the URL /index.php?r=post/view&id=100
represents the route post/view and the id query parameter 100. The default URL format does not require any configuration of the [[yii\web\UrlManager|URL manager]] and works in any Web server setup.
The pretty URL format uses the extra path following the entry script name to represent the route and the associated query parameters. For example, the extra path in the URL /index.php/post/100 is /post/100 which may represent the route
post/view and the id query parameter 100 with a proper [[yii\web\UrlManager::rules|URL rule]]. To use the pretty URL format, you will need to design a set of [[yii\web\UrlManager::rules|URL rules]] according to the actual requirement about how the URLs should look like.
You may switch between the two URL formats by toggling the [[yii\web\UrlManager::enablePrettyUrl|enablePrettyUrl]]
property of the [[yii\web\UrlManager|URL manager]] without changing any other application code.