YII2 Beginner's Guide to Practical tutorials-Simple Blog management system, YII2 beginner _php Tutorial

Source: Internet
Author: User
Tags php framework yii

YII2 Beginner's Guide to Practical tutorials-Simple Blog management system, YII2 beginners


1. Introduction

The Quick Start Guide provides a basic introduction to the YII2 framework, including database migration, GII operations, AR models, routing, validation, views, and more. If you're a Yii2 novice or even unfamiliar with the PHP framework, this will be a good starting point for you. If you have already used and mastered the YII2 Framework Foundation, you can expect the Yii2 Advanced tutorial (I'll update later).

To demonstrate the basic use of YII2 features, I will lead you to build a simple blog management system.

The complete code for this tutorial will be published on GitHub later on.

2. Installation

We have written earlier about the Yii2 full version of the installation tutorial, you can click for reference, here only steps, no longer do the necessary explanation.

Global require "fxp/composer-asset-plugin:~1.1.1"composer create-project yiisoft/yii2-app-advanced Advanced 2.0.8CD advancedphp Init
#之后构建本地环境, we configure Advanced.dev to point to the Frontend/web directory

3. Prepare the database.

When developing and maintaining a database-driven application, the structure of the database changes as the code changes. For example, in the process of developing an application, a new table is added and must be added; After the application is deployed to the production environment, an index needs to be established to improve the performance of the query, and so on. Because the source code also often needs to change when a database structure changes, YII provides a database migration feature that records changes to the database so that the database and source code are versioned together.

In this example, we use yii migrate commands to generate a data table migration for blog blogs:

Yii migrate/create create_blog_table

The migration file generated by this command is located in the Advanced\console\migrations directory, and you may have noticed that the Yii migrate command has added the primary key ID and table name to us in the migration file. Next we'll edit the file to modify the table name and add more columns to the Data table blog:

 Php Useyii\db\migration;/** * Handles the creation for table ' blog_table '.*/classM160525_153315_create_blog_tableextendsmigration{/** * @inheritdoc*/     Public functionUp () {$this->createtable (' blog ', [            ' id ' =$this->primarykey (), ' title ' = =$this-string(+)->notnull ()->defaultvalue ("), ' content ' =$this->text (), ' create_time ' =$this->datetime (),        ]); }    /** * @inheritdoc*/     Public functionDown () {$this->droptable (' blog '); }}

Before running the migration, we first configured the database, open the common\config\main-local.php file, we see the DB configuration under Components, refer to the following configuration.

' components ' + =    [' db '        = ' = ' class ' = ' yii\db\connection ',        //  You need to manually create dbname before modifying host and dbname to '        dsn ' = ' mysql:host=localhost;dbname=advanced ',        // log in to the database account        ' Username ' + ' root ',        // Login Database password        ' password ' = ' ",        ' charset ' = ' utf8 ',     ],    // Other code],

After the database is configured, run the following command to run migrate

./yii Migrate

The period will let us confirm, yes after the return, the command will create a migration file (console\migrations directory) defined in all the data tables, after the execution of the command open the database will find that our blog table has been created, which contains the columns defined in the migration.

4. Use the GII to generate AR models and crud

The GII is a module in YII2 and is a highly customizable and extensible code generation tool. Using it can greatly improve our development efficiency, and later I will explain how to use the GII to customize the template and program code we need. The GII is turned on by default if you are in the process of installing the development environment as we chose. This means that we can use it without any further configuration. You can also open the file advanced\frontend\config\main-local.php to view the configuration code.

if (! yii_env_test) {    // Other code    $config [' Bootstrap '] [] = ' gii ';     $config [' Modules '] [' gii '] = [        ' class ' = ' Yii\gii\module ',    ];}

The GII module is then accessed via address http://advanced.dev/index.php?r=gii (at the outset we configured Advanced.dev to point to the Frontend/web directory), Its features help us to generate the series of code necessary for this operation.

4.1 Generating AR Model classes

Models are part of the MVC design pattern, and using models not only makes it relatively easy and convenient for us to access data, but also helps us to deal with complex business and logic. For more about the model description, you can refer to the relevant manuals or documentation, there are any questions you can also leave a message below.

Let's go back and click Model Generator start on the GII page to generate an AR model class like this.

4.2 Generating CRUD Code

The so-called crud is nothing more than create read update delete, which is created, read, updated, and deleted. Contains basic operations for common web development. If you have just generated the model with the GII, it would be nice to click on the left menu crud generator to generate crud like this.

For more information about the GII, you can refer to the detailed steps of the YII2 gii.

So far, we've built a Model,curd series of operations with the GII.

Good tip: In real-world development, background management should use the GII to help develop and quickly improve development results.

In accordance with the above, we will generate 9 files in the relevant directory as follows:

Common\models\blog. Phpcommon\models\blogsearch. Phpfrontend\controllers\blogcontroller. Phpfrontend\views\blog\_form. Phpfrontend\views\blog\_search. phpfrontend\views\blog\create. Phpfrontend\views\blog\index. phpfrontend\views\blog\update. phpfrontend\views\blog\view. php

Then you can see the blog specific page information by routing access to Http://advanced.dev/index.php?r=blog.

5. Add a Blog

5.1 Preparing before adding

[Considering the current domestic web site most of the collection of articles very frequently, not to mention the original source, the original author would like to see the original text, in case there are any problems can not update all articles, to avoid misleading! ]

Continue Reading

http://www.bkjia.com/PHPjc/1133415.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133415.html techarticle Yii2 Beginner's Guide to Practical tutorials-Simple Blog management system, YII2 Beginner 1, Introduction Quick Start Guide provides a basic introduction to the YII2 framework, including database migration, GII operations ...

  • Related Article

    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.