yii2实战教程之新手入门指南-简单博客管理系统,yii2新手入门
yii2实战教程之新手入门指南-简单博客管理系统,yii2新手入门
1、简介
快速入门指南会对Yii2框架做一个基本介绍,包括数据库迁移、gii操作、AR模型、路由、验证、视图等等。如果你是个Yii2新手甚至之前对PHP框架也很陌生,那么这里将会成为你的良好起点。如果你已经使用并且掌握了Yii2框架基础,可以期待Yii2高级篇教程(后面我会更新)。
为了演示Yii2特性的基本使用,我将会带领大家构建一个简单的博客管理系统。
本教程完整的代码后期会公开于github上。
2、安装
我们前面写过关于Yii2完整版的安装教程,你可以点击进行参考,这里只做步骤,不再做必要的讲解。
composer <span>global</span> <span>require</span> "fxp/composer-asset-plugin:~1.1.1"<span> composer create</span>-project yiisoft/yii2-app-advanced advanced 2.0.8<span> cd advanced php init<br /></span>
#之后构建本地环境,我们配置advanced.dev指向frontend/web目录
3、准备好数据库
在开发和维护一个数据库驱动的应用程序时,数据库的结构会随代码的改变而改变。例如,在开发应用程序的过程中,会增加一张新表且必须得加进来; 在应用程序被部署到生产环境后,需要建立一个索引来提高查询的性能等等。 因为一个数据库结构发生改变的时候源代码也经常会需要做出改变,Yii 提供了一个 数据库迁移 功能,该功能可以记录数据库的变化, 以便使数据库和源代码一起受版本控制。
在本例中,我们使用yii migrate
命令生成博客 blog 对应的数据表迁移:
yii migrate/create create_blog_table
该命令生成的迁移文件位于 advanced\console\migrations 目录,可能你已经注意到了,yii migrate 命令已经在迁移文件中为我们添加了主键ID和表名,接下来我们要编辑该文件修改表名以及添加更多的列到数据表blog:
<?<span>php </span><span>use</span><span> yii\db\Migration; </span><span>/*</span><span>* * Handles the creation for table `blog_table`. </span><span>*/</span> <span>class</span> m160525_153315_create_blog_table <span>extends</span><span> Migration { </span><span>/*</span><span>* * @inheritdoc </span><span>*/</span> <span>public</span> <span>function</span><span> up() { </span><span>$this</span>->createTable('blog',<span> [ </span>'id' => <span>$this</span>->primaryKey(), 'title' => <span>$this</span>-><span>string</span>(100)->notNull()->defaultValue(''), 'content' => <span>$this</span>->text(), 'create_time' => <span>$this</span>->datetime(),<span> ]); } </span><span>/*</span><span>* * @inheritdoc </span><span>*/</span> <span>public</span> <span>function</span><span> down() { </span><span>$this</span>->dropTable('blog'<span>); } }</span>
运行迁移之前,我们先把数据库配置一番,打开common\config\main-local.php文件,我们看到components下面的db配置,参考如下配置就好
'components' =><span> [ </span>'db' =><span> [ </span>'class' => 'yii\db\Connection', <span>//</span><span> 修改host 和dbname 之前需要手动创建了dbname才可以</span> 'dsn' => 'mysql:host=localhost;dbname=advanced', <span>//</span><span>登录数据库的账号</span> 'username' => 'root', <span>//</span><span>登录数据库的密码</span> 'password' => '', 'charset' => 'utf8',<span> ]</span>, <span>//</span><span> other code</span> ],
数据库配置好了之后,运行执行如下命令运行migrate
./yii migrate
期间会让我们确认,yes后回车即可,该命令会为我们创建迁移文件(console\migrations目录)中定义的所有数据表,执行完该命令打开数据库会发现,我们的blog表已经创建了,其中包含了在迁移中定义的列。
4、使用gii生成AR模型和CRUD
gii是yii2中的一个模块,是一种高度可定制和可扩展的代码生成工具。使用它可以大幅提高我们的开发效率,后面我会也会讲解如何用gii定制我们需要的模版以及程序代码。如果你在安装的过程中,像我们一样选择的是开发环境,gii默认是开启的。也就是说我们无需再进行配置便可以使用。你也可以打开文件 advanced\frontend\config\main-local.php查看配置代码。
<span>if</span> (!<span>YII_ENV_TEST) { </span><span>//</span><span> other code</span> <span>$config</span>['bootstrap'][] = 'gii'<span>; </span><span>$config</span>['modules']['gii'] =<span> [ </span>'class' => 'yii\gii\Module',<span> ]; }</span>
接着通过地址 http://advanced.dev/index.php?r=gii 访问gii模块(在一开始我们配置了advanced.dev指向了frontend/web目录),借助其特性帮助我们生成此次操作所必需的一系列代码。
4.1生成AR模型类
模型是MVC设计模式中的一部分,使用模型不仅能让我们存取数据变得相对简单和方便,更多地协助我们处理复杂的业务和逻辑。关于更多的有关模型的描述,可以参考相关手册或文档,有任何问题你也可以下方留言。
我们回过头来点击gii页面上的Model Generator start,像下面这样生成AR模型类。
4.2生成CRUD代码
所谓的CRUD无非就是Create Read Update Delete,也就是创建、读取、更新和删除。包含了常见Web开发的基本操作。如果你刚刚用gii生成了Model,此时点击左侧菜单CRUD Generator像下面这样生成crud再好不过了。
关于更多gii的操作你可以参考yii2 gii的详细操作步骤。
目前为止,我们借助gii生成了model,curd一系列操作。
有好提示:实际开发中,后台管理理应利用gii协助开发,可快速提高开发效果。
按照以上操作,我们会在如下相关目录生成9个文件
common\models\Blog.<span>php common\models\BlogSearch</span>.<span>php frontend\controllers\BlogController</span>.<span>php frontend\views\blog\_form</span>.<span>php frontend\views\blog\_search</span>.<span>php frontend\views\blog\create</span>.<span>php frontend\views\blog\index</span>.<span>php frontend\views\blog\update</span>.<span>php frontend\views\blog\view</span>.php
接着可以通过路由访问http://advanced.dev/index.php?r=blog看到blog具体页面信息。
5、添加博客
5.1添加前准备
[考虑目前国内网站大部分采集文章十分频繁,更有甚者不注明原文出处,原作者更希望看客们查看原文,以防有任何问题不能更新所有文章,避免误导!]
继续阅读

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

PHP Practice: Code Example to Quickly Implement the Fibonacci Sequence The Fibonacci Sequence is a very interesting and common sequence in mathematics. It is defined as follows: the first and second numbers are 0 and 1, and from the third Starting with numbers, each number is the sum of the previous two numbers. The first few numbers in the Fibonacci sequence are 0,1,1.2,3,5,8,13,21,...and so on. In PHP, we can generate the Fibonacci sequence through recursion and iteration. Below we will show these two

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code
