Yii2 hands-on tutorial-Beginner's Guide-simple blog management system, yii2 beginner's Portal-PHP Tutorial

Source: Internet
Author: User
Yii2 Getting Started Guide-simple blog management system, yii2 getting started. Yii2 hands-on tutorial-Getting Started Guide-simple blog management system, yii2 beginners 1. introduction quick start guide will give a basic introduction to the Yii2 framework, getting started with database migration and gii yii2 tutorial-simple blog management system and yii2
1. Introduction

The quick start guide provides a basic introduction to the Yii2 framework, including database migration, gii operations, AR models, routing, verification, and views. If you are a beginner in Yii2 or even a stranger to the PHP framework, it will become a good starting point for you. If you have already used and mastered the basics of the Yii2 framework, you can look forward to the Yii2 advanced tutorial (which will be updated later ).

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

The complete code of this tutorial will be published on github later.

2. Installation

We have previously written an installation tutorial for the Yii2 full version. you can click here for reference. here, we only perform the steps and do not need to explain them any more.

composer global require "fxp/composer-asset-plugin:~1.1.1"composer create-project yiisoft/yii2-app-advanced advanced 2.0.8cd advancedphp init
# Then build the local environment. we configure advanced. dev to point to the frontend/web directory.
3. prepare the database

When developing and maintaining a database-driven application, the database structure changes with the code. For example, during application development, a new table is added and must be added. after the application is deployed to the production environment, an index is required to improve the query performance. Because the source code often needs to be changed when the structure of a database changes, Yii provides a database migration function that records database changes, so that the database and source code are controlled by the version.

In this example, we useyii migrateCommand to generate the data table migration corresponding to the blog:

yii migrate/create create_blog_table

The migration file generated by this command is located in the advanced \ console \ migrations Directory. you may have noticed that the yii migrate command has added the primary key ID and table name for us in the migration file, next, edit the file to modify the table name and add more columns to the data table blog:

 createTable('blog', [            'id' => $this->primaryKey(),            'title' => $this->string(100)->notNull()->defaultValue(''),            'content' => $this->text(),            'create_time' => $this->datetime(),        ]);    }    /**     * @inheritdoc     */    public function down()    {        $this->dropTable('blog');    }}

Before running the migration, we first configure the database, open the common \ config \ main-local.php file, we can see the db configuration under components, refer to the following configuration is good

'Components' => ['DB' => ['class' => 'yii \ db \ connection ', // you must manually create a dbname before modifying the host and dbname to 'dsn '=> 'MySQL: host = localhost; dbname = advanced ', // the account for logging on to the database 'username' => 'root', // The password for logging on to the database 'password' => '', 'charset' => 'utf8',], // other code],

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

./yii migrate

During this period, let's confirm, yes, and press Enter. this command will create all the data tables defined in the migration File (console \ migrations directory) for us. after executing this command to open the database, we will find that, our blog table has been created, including the columns defined in the migration.

4. use gii to generate the AR model and CRUD

Gii is a module in yii2 and a highly customizable and scalable code generation tool. Using it can greatly improve our development efficiency. I will also explain how to use gii to customize the template and program code we need. If you select a development environment like we did during installation, gii is enabled by default. That is to say, we can use it without 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',    ];}

Then through the address http://advanced.dev/index.php? R = gii access the gii module (we configured advanced. dev to point to the frontend/web directory at the beginning) and used its features to help us generate a series of code required for this operation.

4.1 generate an AR model class

The model is part of the MVC design pattern. Using the model not only makes it relatively simple and convenient for us to access data, but also helps us to process complicated business and logic. For more information about the model, see the relevant manual or documentation. if you have any questions, leave a message below.

Let's go back and click Model Generator start on the gii page to generate the AR Model class as follows.

4.2 generate CRUD code

The so-called CRUD is nothing more than Create Read Update Delete, that is, creating, reading, updating, and deleting. Includes basic operations for common Web development. If you have just used gii to generate a Model, you can click the CRUD Generator in the left-side menu to generate a crud as follows.

For more gii operations, refer to the detailed operation steps of yii2 gii.

So far, we have used gii to generate a series of model and curd operations.

Tip: in actual development, background management should use gii to assist in development, which can quickly improve the development effect.

According to the above operations, we will generate nine files in the following directories:

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 access http://advanced.dev/index.php through routing? R = the blog page information is displayed.

5. add a blog 5.1 before adding a blog

[Considering that most of the articles collected on Chinese websites are very frequent at present, the author does not specify the source of the original article. the original author prefers the readers to check the original article to avoid updating all the articles due to any problems and avoid misleading.]

Continue reading

Remark 1. the quick start guide will provide a basic introduction to the Yii2 framework, including database migration and gii operations...

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.