installation of the YII2 framework we have mentioned in the previous article that we are beginning to understand the YII2 framework
Powerful YII2 Framework Online Guide: http://www.yii-china.com/doc/detail/1.html?postid=278 or
http://www.yiichina.com/doc/guide/2.0
Application Structure of YII2:
Catalogue:
The
advance version is characterized by a pre-assigned three modules in the root directory, namely the foreground, background, and console modules. 1.backend It is mainly used for the management of the background, webmaster to manage the entire system. The
Assets directory is used to hold the front-end resource bundle PHP classes. There is no need to know what is the front-end resource package, as long as the general knowledge is used to manage CSS, JS and other front-end resources can be. Config is used to store the configuration file for this application, including the master profile main.php and the global parameter profile params.php. Models views Controllers 3 directories for data model classes, view files, controller classes, respectively. This is the core of our code and the directory where we work most. The widgets directory is used to store class files for some commonly used small pendants. The tests directory is used to store test classes. Web directory as you can see from the name, this is a directory that the Web server can access. In addition to this directory, all other directories should not be exposed to web users. This is the need for security. Runtime This directory is required to chmod 777, that is, allow the Web server to have full permissions, because it may involve writing temporary files and so on. However, a directory is not visible to web users. In other words, permissions are given, but not accessible to web users.
2.frontend
The front-end application of our target end-user-provided primary interface. In fact, the foreground and backstage are the same, just a division of our logic.
OK, here's the problem. For standalone applications, such as frontend backend console , their content is placed under their respective directories, and their operation is bound to use the YII framework vendor program. How they relate. The secret, or the secret of the directory structure of the entire Yii application, is contained in a legendary place called the entry file.
1
2
3
4
5
6
7
8
9
18 |
<?phpdefined (' yii_debug ') or define (' Yii_debug ', true); Defined (' yii_env ') or define (' yii_env ', ' dev '); Require (__dir__. '/.. /.. /vendor/autoload.php '); Require (__dir__. '/.. /.. /vendor/yiisoft/yii2/yii.php '); Require (__dir__. '/.. /.. /common/config/bootstrap.php '); Require (__dir__. '/.. /config/bootstrap.php '); $config = Yii\helpers\arrayhelper::merge ( Require (__dir__. '/.. /.. /common/config/main.php '), Require (__dir__. '/.. /.. /common/config/main-local.php '), Require (__dir__. '/.. /config/main.php '), Require (__dir__. '/.. /config/main-local.php ')); $application = new Yii\web\application ($config); $application->run (); |
The 3.console console application contains the console commands required by the system.
The following is the global public folder 4.common
Where: config is a generic configuration that will be used for the front-end and command-line. mail is the app's front-end and command-line mail-related layout files. models is a data model that can be used by both the front and the command line. This is also the most important part of common . The files contained in the
Common directory (Common) are used for sharing among other applications. For example, each application may need to access the database using ActiveRecord. Therefore, we can place the AR model class in the public (common) directory. Similarly, if you use some helper or part classes (widgets) in multiple applications, we should also place them under the Common directory (common) to avoid duplicate code. As we will soon explain, applications can also share part of the shared configuration. Therefore, we can also store common configurations common in the Config directory.
When developing a large project development cycle, we need to constantly adjust the database structure. For this reason, we can also use the database migration (DB migrations) feature to keep track of database changes. We also put all DB migrations (Database Migration) directories under the Common (common) directory.
5.environment Each YII environment is a set of configuration files that contains the portal script index.php and various configuration files. In fact they are all placed under the/environments directory.
From the directory structure above, you can see that there are 3 things in the Environment directory: directory dev directory prod file index.php
The dev and prod structures are the same, with 4 directories and 1 files, respectively: Frontend directory for foreground applications, contains config directory for configuration files and Web directory backend directory for Web portal scripts for background applications, content and front End the same console directory, for command-line applications, contains only the Config directory, because the command-line app does not require a Web portal script and therefore does not have a web directory. The common directory, used for various web applications and command-line application common environment configuration, contains only the Config directory, because different applications cannot share the same portal script. Note that this level of common is lower than the level of the environment, that is, his generic, is only a certain environment in general, not all of the environment in general. Yii file, which is the entry script file for the command line application.
They are also common to the web and config directories that are scattered everywhere. All web directories, are stored in the Web application of the portal script, a index.php and a test version of the Index-test.php General Config directory, stored, are local configuration information main-local.php and params-local.php
6.vendor vendor. This directory, literally, is a variety of third-party programs. This is the storage directory for other programs installed by composer, including the YII framework itself, which is also placed under this directory. If you add a new program to the Composer.json directory that needs to be installed, the next time you call composer, the newly installed directory will be installed under this vendor.
Below are also some of the less commonly used folders 7.vagrant 8.tests
Entry file:
1. Entry file path:
http://127.0.0.1/yii2/advanced/frontend/web/index.php
Each app has a portal script Web/index. PHP, which is the only PHP script that can be accessed throughout the app. One application processes the request as follows:
1. The user initiates the request to the portal script web/index.php.
2. The portal script loads the application configuration and creates an application instance to process the request.
3. Apply the route through the request component to resolve the request.
4. The application creates a controller instance to process the request.
5. The controller creates an action instance and executes the filter against the action.
6. If any one of the filters fails to return, the operation exits.
7. If all filters are passed, the operation will be performed.
8. The operation will load a data model, perhaps from a database.
9. The operation renders a view that provides the data model to it.
10. The render result is returned to the response component.
11. The response component sends rendering results to the user's browser to see the middle of the model-view-controller, commonly said Mvc. The portal script does not process the request, but instead gives the request to the application principal, and when the request is processed, the controller is used, and if it is used in the database, the model is accessed, and if the processing request is completed and returned to the user information, the content in the view is returned to the user.
2, why our access method will appear URL encryption.
we found the file: vendor/yiisoft/yii2/web/urlmanager.php
Return "$baseUrl/{$route} {$anchor}";
} else {
$url = "$baseUrl? { $this