Learn from zero YII2 Framework (vi) Advanced Application Template _php instance

Source: Internet
Author: User
Tags yii

Advanced Application Templates
This template is used in large team development projects, and the background is isolated from the foreground to facilitate deployment across multiple servers. Because of some of the new features of YIi2.0, this program template has a deeper function. Provide a basic database support, registration, password recovery and other functions.

Installation

Can be installed through composer.
If composer is not installed, first install

Curl-s Http://getcomposer.org/installer | Php

Then use the following command to obtain the

PHP composer.phar create-project--prefer-dist--stability=dev yiisoft/yii2-app-advanced/path/to/yii-application

You can also download the compressed file directly: Yii 2 with Advanced application Template (Beta)
https://github.com/yiisoft/yii2/...-app-2.0.0-beta.tgz

Begin

Once the installation is complete, it needs to be initialized.

Execute INIT, select development environment (DEV)
php/path/to/yii-application/init
Create new database, set common/config/ The COMPONENTS.DB database information in main-local.php
use console commands to migrate databases
yii migrate
set the Web server's root directory
foreground/path/to/ yii-application/frontend/web/the corresponding URL for the http://frontend/
background/path/to/yii-application/backend/web/corresponds to the URL http://backend/

Directory structure

There are several subdirectories below the root directory

backend--Background Web Program
common--Public Files
console--Console Program
environments--Environment Configuration
frontend--Foreground Web Program
The root directory contains a few files below

Files and directories that are ignored in gitignore--git versioning, and add them to this file if you have some files you don't want to have in your source code.
composer.json--this will be described below
init--the initialization scripts described in composer
init.bat--is the same as above, but under Windows
liense.md--, I won't tell you this.
readme.md--ditto
REQUIREMENTS.PHP--YII Operating Environment Requirements test files
yii--Console Program boot file
Yii.bat--windows the stuff underneath.

System-defined path alias

@yii--The directory of the framework.
@app-The basic path of the application that is currently running.
@common-Public file directory.
@frontend-Front-End Web application directory.
@backend--back-end Web application directory.
@console-Console directory.
@runtime-The runtime directory of the currently running Web application
@vendor-Basic Framework directory.
@web-the URL of the Web application that is currently running
@webroot-the Web root directory of the Web application that is currently running.

Application

This template contains three applications, foreground, background, and console. The front desk is usually shown to end users, the project itself. Background is the Administrator Control Panel, including analysis and similar functions. Console is mainly used to do some scheduled tasks and some simple server management, but also can be used to deploy applications, database migration, resource management, and so on.

The common directory provides some common files that can be used for multiple applications, such as the user model.

Both the foreground and the background are Web applications, they all contain a web directory, the root of the Web, and you have to point to this directory when you deploy the server.
Each application has its own namespace and the corresponding alias. By the same token, common also has its own namespace and corresponding alias.

Configuration and development environment

In normal development, there are multiple problems with setting up a configuration file directly

Each team member has its own configuration options. If committing such a configuration will affect other team members.
The product database password and API key should not be in the code warehouse.
In the case of multiple servers: development, testing, production, each server should have its own configuration.
Defining all the configuration options in each case is repetitive and takes too much time to maintain it.

To solve these problems, Yii introduces a very simple concept of the environment. Each environment is represented by a set of files in the environment directory. The init command is used for switching between different environments. It simply replicates the root directory of all applications from the environment directory.

Typically, the environment contains application boot files such as index.php and configuration files with-local.php suffixes. These have been added to the. Gitignore and will not be added to the source repository.
In order to avoid duplicate configuration files overwriting each other. For example, the foreground application reads the configuration in the following order:

common/config/main.php
common/config/main-local.php
frontend/config/main.php
frontend/config/ main-local.php

The parameter files are read in the following order

common/config/params.php
common/config/params-local.php
frontend/config/params.php
frontend/config /params-local.php

The file configuration read later overwrites the previous configuration

The entire flowchart is as follows

Configure Composer

After the application installation is complete, you can set the Composer.json below the directory

{"Name": "yiisoft/yii2-app-advanced", "description": "Yii 2 Advanced Application Template", "keywords": ["Yii", "F Ramework "," Advanced "," Application Template "]," homepage ":" http://www.yiiframework.com/"," type ":" Project "," Lice NSE ":" Bsd-3-clause "," support ": {" Issues ":" Https://github.com/yiisoft/yii2/issues?state=open "," forum ":" http 
    ://www.yiiframework.com/forum/", wiki": "http://www.yiiframework.com/wiki/", "IRC": "Irc://irc.freenode.net/yii", "Source": "Https://github.com/yiisoft/yii2"}, "minimum-stability": "Dev", "require": {"PHP": ">=5.4.0" , "Yiisoft/yii2": "*", "Yiisoft/yii2-swiftmailer": "*", "yiisoft/yii2-bootstrap": "*", "yiisoft/yii2-debug ":" * "," yiisoft/yii2-gii ":" * "}," scripts ": {" Post-create-project-cmd ": [" Yii\\composer\\installer:: SetPermission "]}," Extra ": {" writable ": [" Backend/runtime "," Backend/web/assets "," Conso Le/runtime "," consOle/migrations "," Frontend/runtime "," Frontend/web/assets "]}}
 

First, modify some basic information. For example, name, description, keywords, home page and so on.
You can also add more apps to your needs. These packages are all from packagist.org and are free to browse all the code.
After modifying Composer.json, you can run

PHP Composer.phar Update--prefer-dist

, and then you can start using it once the download and installation is complete. Automatically loaded classes will be processed automatically.

Create a link from the back end to the front end

Typically, you need to connect to a front-end application from a back-end application. Because the front-end application may contain its own URL management rules, you need to add a background URL management rule with a different name.

return [
  ' components ' => ['
    urlmanager ' => [
      //# is your normal backend URL manager config
    ],
    ' Urlmanagerfrontend ' => [
      //Is your frontend URL Manager config
    ],
  ]
;

After that, you can use the foreground URL like this

echo Yii:: $app->urlmanagerfrontend->createurl (...);

Original link: http://www.yiifans.com/forum.php?mod=viewthread&tid=25
(Source: yii2| Yii enthusiasts Chinese Community portal)

Translation Source: http://www.yiiframework.com/doc-2.0/guide-apps-advanced.html
(Yii Framework 2.0 API decumentation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.