Home >PHP Framework >YII >Getting Started Guide to Yii Framework: Basics and Applications

Getting Started Guide to Yii Framework: Basics and Applications

王林
王林Original
2023-06-21 08:58:363032browse

Introduction:

Yii is an efficient, secure, and easily scalable PHP web application framework for rapid development of modern web applications. The Yii framework source code is licensed under the MIT license. You can use it for free in commercial projects as long as you follow the terms of the license.

Article:

  1. Introduction to Yii

The Yii application framework is a web application based on the MVC (Model-View-Controller) pattern frame. It is an object-oriented framework designed to simplify the development process and improve the performance and security of web applications.

Yii framework provides a series of components and tools that can assist in the rapid development of advanced web applications. The goal of the Yii framework is to provide an efficient, secure and easy-to-use framework so that developers can save time and effort when using it.

  1. Yii installation and configuration

Before you start using the Yii framework, you first need to install it. The Yii framework can be installed through the composer command. You need to ensure that the composer command has been installed. The following are the steps on how to install the Yii framework:

composer require yiisoft/yii2-app-basic

After the installation is complete, you can configure the Yii framework through the configuration file. By default, Yii framework uses the configuration file config/web.php. This file can be used to configure all components of the application, such as database components, router components, etc.

  1. The basic structure of Yii

The basic structure of Yii framework is as follows:

project/
    assets/                 用于存储自动生成的Web资源
    commands/               包含项目命令文件
    config/                 包含应用程序的配置文件
        web.php             Web应用程序配置文件
    controllers/            包含项目的控制器类
    models/                 包含与数据库表对应的模型类
    runtime/                用于存储临时文件和缓存文件
    tests/                  用于存储单元测试和功能测试文件
    vendor/                 包含应用程序的依赖项
    views/                  包含Web应用程序的视图文件
    web/                    包含可以通过Web访问的文件(包括index.php前台文件)
  1. Yii routing

The routing controller parses the URL and forwards the request to the correct controller and method. The Yii framework's routing provides a variety of flexible options, including traditional URL paths, query strings, and rule-based routing. In Yii framework, routing rules can be declared using:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '<controller:w+>/<id:d+>'=>'<controller>/view',
        '<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action>',
        '<controller:w+>/<action:w+>'=>'<controller>/<action>',
    ],
],
  1. Yii’s Model

Yii framework’s models are objects associated with database tables, they can Used to perform various operations such as reading and writing data. The Yii framework's models implement the Active Record pattern and provide some useful features such as data validation and data correlation.

The following is an example of the Yii framework model:

class User extends yiidbActiveRecord
{
    public static function tableName()
    {
        return 'user';
    }
 
    public function rules()
    {
        return [
            [['username', 'email'], 'required'],
            [['username', 'email'], 'unique'],
            [['email'], 'email'],
        ];
    }
 
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'email' => 'Email',
        ];
    }
}
  1. Yii’s view and layout

The view of the Yii framework is to display data and user interaction place. They can contain HTML, CSS and JavaScript code, as well as PHP code for outputting data and interacting with the user. Views can use layouts to share common snippets and view elements.

The following is an example of Yii framework views and layouts:

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
 
<div class="container">
    <?= $content ?>
</div>
 
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
  1. Yii's controller

Yii framework's controller is responsible for handling web applications user requests and interacts with models and views. The controller contains multiple actions, each handling a page request. Each operation can render a view or output data directly.

The following is an example of Yii framework controller:

class UserController extends yiiwebController
{
    public function actionIndex()
    {
        $users = User::find()->all();
        return $this->render('index', ['users' => $users]);
    }
 
    public function actionView($id)
    {
        $user = User::findOne($id);
        return $this->render('view', ['user' => $user]);
    }
 
    public function actionCreate()
    {
        $user = new User();
        if($user->load(Yii::$app->request->post()) && $user->save()){
            return $this->redirect(['view', 'id' => $user->id]);
        }
        return $this->render('create', ['user' => $user]);  
    }
 
    public function actionUpdate($id)
    {
        $user = User::findOne($id);
        if($user->load(Yii::$app->request->post()) && $user->save()){
            return $this->redirect(['view', 'id' => $user->id]);
        }
        return $this->render('update', ['user' => $user]);
    }
 
    public function actionDelete($id)
    {
        $user = User::findOne($id);
        $user->delete();
        return $this->redirect(['index']);
    }
}

Conclusion:

The above is the introduction, installation, basic structure, routing, model, view, layout of Yii framework and the basic knowledge and applications of controllers, which are the basis for learning the Yii framework. Armed with this knowledge, you can start building complex web applications using the Yii framework.

The above is the detailed content of Getting Started Guide to Yii Framework: Basics and Applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn