Yii2 框架 之所以称之为高效快速开发的一款框架,是因为有一个神奇的工具Gii
用过Yii1框架的Coder都知道,Gii可以为你快速生成代码,也就是说搭建一个可以增删改查的WebApp可能一行代码都不用写。
当然作为Coder,不写代码怎么能实现我们想要的功能呢。
上次介绍了如何安装Yii框架,本次介绍一下如何使用gii工具快速实现CRUD功能。
框架安装完成后可以通过如下链接访问Gii工具
http://localhost/yii2test/backend/web/index.php?r=gii
Yii2的Gii工具使用了Bootstrap,对比Yii1看起来漂亮了许多。
yii2框架提供的生成工具有6个:Model,CRUD,Controller,Form,Module,Extension
我安装了一个yii2-kartikgii插件,此插件是用来生成Kartik模式的CRUD方法的。
本次就拿Kartik Crud配合默认生成工具来讲解。
模块的生成
如果你想实现应用模块化, 那么Module Generator能帮助到你。点击Module Generator生成模块
生成一个名为“user”的模块
点击Preview会看到Gii会帮助我们生成三个文件,可以点击链接预览代码。
点击Generate生成代码。
代码生成完毕,需要配置文件backend/config/main.php,把模块添加到应用里。
浏览器输入如下地址:
http://localhost/yii2test/backend/web/index.php?r=user
访问的是user模块
提示你
This is the view content for action "index". The action belongs to the controller "backend/modules/user/controllers/DefaultController" in the "user" module.
You may customize this page by editing the following file:
D:/WAMP/www/yii2test/backend/modules/user/views/default/index.php
这个界面内容访问的是模块user下的defaultController执行的是index这个action。
你可能需要自定义这个页面通过修改模块下的views文件index.php
模型的生成
模型的生成首先得配置数据库配置文件,路径:yii2test/common/config/main-local.php。
配置好数据库,我们使用Model Generator来生成对应的模型。
新闻热点
疑难解答