Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on the method of learning YII2 framework advanced application templates from scratch

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the method tutorial of learning YII2 framework advanced application template from scratch". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the method tutorial of learning YII2 framework advanced application templates from scratch.

Advanced Application template

This template is used in large team development projects, and the background is separated from the foreground to facilitate deployment on multiple servers. Due to some new features of YIi2.0, the function of this program template is a little deeper. Provides basic database support, registration, password recovery and other functions.

Installation

Can be installed through Composer

If you do not have Composer installed, install it first

Curl-s http://getcomposer.org/installer | php

Then use the following command to get

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

Start

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

Execute init, select the development environment (dev) php / path/to/yii-application/init to create a new database, set the components.db database information in common/config/main-local.php, use the console command to migrate the database yii migrate, set the root directory foreground / path/to/yii-application/frontend/web/ of the web server, the url corresponding to http://frontend/ background / path/to/yii-application/backend/web/, and the url corresponding to web server is http://backend/.

Directory structure

There are these subdirectories under the root directory

Backend-- background web program

Common-- public files

Console-- console program

Environments-- environment configuration

Frontend-- foreground web program

There are also some files under the root directory

Gitignore-files and directories ignored in git version control. If you have files you don't want to add to your source code, add it to this file.

Composer.json-- will be described below.

Initialization script described by init-- in Composer

Init.bat-- is the same as above, but under Windows

LIENSE.md--, let's not talk about this.

README.md-- ditto

Requirements.php--Yii operating environment requires test files

Yii-- console program boot file

What's under yii.bat--Windows?

System-defined path alias

@ yii-- the directory of the framework.

The basic path to the currently running application of @ app--.

@ common-Public file directory.

@ frontend-- front-end web application directory.

@ backend-- the backend 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 currently running web application

@ webroot-- the web root directory of the currently running web application.

Application program

This template contains three applications, foreground, background, and console. The front desk is usually presented to the end user, that is, the project itself. In the background is the administrator control panel, which contains analysis and similar functions. The 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 common files that can be used in multiple applications, such as the User model.

Both foreground and background are web applications that contain a web directory, the root of web, that you have to point to when you deploy the server.

Each application has its own namespace and corresponding aliases. Similarly, common has its own namespace and corresponding aliases.

Configuration and development environment

In normal development, there are several problems with setting up the configuration file directly

Each team member has its own configuration options. If you submit such a configuration, it will affect other team members.

The product database password and API key should not be in the code repository.

In the case of multiple servers: development, testing, production, each server should have its own configuration.

Defining all configuration options in each case is repetitive, and it takes too much time to maintain it.

To solve these problems, Yii introduces a very simple concept of environment. Each environment is represented by a collection of files under the environment directory. The init command is used to switch between different environments. It simply copies the root directory of all applications from the environment directory.

Typically the environment contains application bootstrap files such as index.php and configuration files with the suffix-local.php. These have been added to .gitignore, so they will not be added to the source repository.

To prevent duplicate configuration files from overwriting each other. For example, the foreground application reads the configuration in the following order:

Common/config/main.phpcommon/config/main-local.phpfrontend/config/main.phpfrontend/config/main-local.php

Parameter files are read in the following order

Common/config/params.phpcommon/config/params-local.phpfrontend/config/params.phpfrontend/config/params-local.php

The file configuration read later will overwrite the previous configuration

The whole flow chart is as follows

Configure Composer

After the application is installed, you can set the composer.json under the directory you want.

{"name": "yiisoft/yii2-app-advanced", "description": "Yii 2 Advanced Application Template", "keywords": ["yii", "framework", "advanced", "application template"], "homepage": "http://www.yiiframework.com/"," type ":" project "," license ":" 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", "console/runtime", "console/migrations", "frontend/runtime", "frontend/web/assets"]}}

First, modify some basic information. Such as name, description, keywords, home page and so on.

You can also add more applications according to your needs. These packages are from packagist.org, and you can browse all the code for free.

After modifying the composer.json, you can run it.

Php composer.phar update-prefer-dist

After the download and installation is complete, you can start using it Automatically loaded classes will be handled automatically.

Create a link from the back end to the front end

Typically, you need to connect from the back-end application to the front-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' = > [/ / here is your normal backend url manager config], 'urlManagerFrontend' = > [/ / here is your frontend URL manager config],]

After that, you can use the foreground url like this

Echo Yii::$app- > urlManagerFrontend- > createUrl (...); at this point, I believe you have a deeper understanding of the tutorial on how to learn advanced application templates of the YII2 framework from scratch, so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report