Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 5.21 KB

workflow.md

File metadata and controls

92 lines (68 loc) · 5.21 KB

Running Applications

After installing Yii, you have a working Yii application that can be accessed via the URL http://hostname/. This section will introduce the application's built-in functionality, how the code is organized, and how the application handles requests in general.

Info: For simplicity, throughout this "Getting Started" tutorial, it's assumed that you have set app/public as the document root of your Web server, and configured the URL for accessing your application to be http://hostname/index.php or something similar. For your needs, please adjust the URLs in our descriptions accordingly.

Note that unlike the framework itself, after the project template is installed, it is all yours. You're free to add or delete code and overall modify it as you need.

Functionality

The application installed contains four pages:

  • the homepage, displayed when you access the URL http://hostname/index.php,
  • the "About" page,
  • the "Contact" page, which displays a contact form that allows end-users to contact you via email,
  • and the "Login" page, which displays a login form that can be used to authenticate end-users. Try logging in with "admin/admin", and you will find the "Login" main menu item will change to "Logout".

These pages share a common header and footer. The header contains the main menu bar to allow navigation among different pages.

You should also see a toolbar at the bottom of the browser window. This is a useful debugger tool provided by Yii to record and display a lot of debugging information, such as log messages, response statuses, the database queries run, and so on.

Additionally, to the web application, there is a console script called yii, which is located in the applications base directory. This script can be used to run background and maintenance tasks for the application, which are described in the Console Application Section.

Application Structure

The most important directories and files in your application are (assuming the application's root directory is app):

app/                    application base path
    composer.json       used by Composer, describes package information
    config/             contains application and other configurations
        console.php     the console application container configuration
        web.php         the Web application container configuration
    src/                source code root
        Command/            contains console command classes
        Controller/         contains controller classes
        Factory/            contains container factories simplifying creation of complicated services
    runtime/            contains files generated by Yii during runtime, such as logs and cache files
    vendor/             contains the installed Composer packages, including the Yii framework itself
        bin/
             yii        the Yii console command execution script
    views/              contains view files
    public/                application Web root, contains Web accessible files
        assets/         contains published asset files (javascript and css) by Yii
        index.php       the entry (or bootstrap) script for the application

In general, the files in the application can be divided into two types: those under app/public and those under other directories. The former can be directly accessed via HTTP (i.e., in a browser), while the latter can not and should not be.

The following diagram shows the static structure of an application.

Static Structure of Application

Each application has an entry script public/index.php which is the only Web accessible PHP script in the application. The entry script is creating an instance an incoming request with the help of one of PSR-7 packages and passes it to application instance. An application contains a set of middleware that are executed sequentially processing the request. The result is passed further to emitter that takes care of sending a response to the browser.

Depending on the middleware used, the application may behave differently. By default, there is a router that, based on URL requested and configuration, chooses a handler that is executed to produce a response.

Request Lifecycle

The following diagram shows how an application handles a request.

Request Lifecycle

  1. A user makes a request to the entry script public/index.php.
  2. The entry script loads the container configuration configuration and creates an application instance to handle the request.
  3. Application is starting to execute middleware configured. One of these is typically a router.
  4. Router finds out what handler to execute based on request and configuration.
  5. The handler may load some data, possibly from a database.
  6. The handler forms a response by using data. Either directly or with the help of the view package.
  7. The response is passed to the emitter that takes care of sending the response to the user's browser.