Yii 简介

YiiFramework

一、关于yii

名字:英文名是yiiframework,中文名叫“易框架”

创始人:薛强

生日:200811

github地址:https://github.com/yiisoft/yii2,目前版本为2.0.8

官方网站:http://www.yiiframework.com/

php版本要求:>=php5.4

优点:面向对象,mvc,惰性加载,DAO/ActiveRecordwidgetscaching,等级式RBACWeb服务,主题化,I18NL10NMigrationgiijQuery整合,安全,错误处理和日志记录,扩展库,详细的文档


注解

惰性加载

Yii如此快速是因为它广泛地使用lazyloading技术。比如,直到第一次使用到这个类,才会包含进来;直到对象第一次访问,才会创造这个对象。


性能对比

Yii是一个高性能的框架,以下的图表展示了与其他流行的PHP框架比较下Yii的高效率。在这个图表中,RPS代表“每秒请求”,描述了此框架每秒执行多少个请求。这个数字越大,此框架的性能越高,我们可以看到,在这个比较中 Yii胜过其他框架


DAO

DAO(Data Access Object)是一个数据访问接口,主要功能:1.DAO用来封装DataSource..就比如,Connectionconn = DAOFactory.createConnection().. 2.DAO也是把对数据库的操作(比如最基本的CRUD操作)全部封装在里面..


Active Record

Active Record则是随着rubyon rails的流行而火起来的一种ORM模式,它是把负责持久化的代码也集成到数据对象中,即这个数据对象知道怎样把自己存到数据库里。

ActiveRecord的主要思想是:

1.每一个数据库表对应创建一个类,类的每一个对象实例对应于数据库中表的一行记录;通常表的每个字段在类中都有相应的Field

2.ActiveRecord同时负责把自己持久化,在ActiveRecord中封装了对数据库的访问,即CURD

3. ActiveRecord是一种领域模型(DomainModel),封装了部分业务逻辑;

ActiveRecord比较适用于:

1.业务逻辑比较简单,当你的类基本上和数据库中的表一一对应时,ActiveRecord是非常方便的,即你的业务逻辑大多数是对单表操作;

2.当发生跨表的操作时,往往会配合使用事务脚本(TransactionScript),把跨表事务提升到事务脚本中;

3. ActiveRecord最大优点是简单,直观。一个类就包括了数据访问和业务逻辑.如果配合代码生成器使用就更方便了;

这些优点使ActiveRecord特别适合WEB快速开发。


Widgets

小物件,比如表单小物件,


I18n

i18n(其来源是英文单词internationalization的首末字符in18为中间的字符数)是“国际化”的简称。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。


L10n
L10N localization的缩写形式,意即在ln之间有10个字母,本意是指软件的“本地化”。指把软件界面和文档翻译成不同国家、地区的语言版本


Migration

在开发和维护一个数据库驱动的应用程序时,数据库的结构会随代码的改变而改变。例如,在开发应用程序的过程中,会增加一张新表且必须得加进来;在应用程序被部署到生产环境后,需要建立一个索引来提高查询的性能等等。因为一个数据库结构发生改变的时候源代码也经常会需要做出改变,Yii提供了一个 数据库迁移 功能,该功能可以记录数据库的变化,以便使数据库和源代码一起受版本控制。

Gii

代码自动生成工具



开始我们的yii2之旅

1.怎么安装yii2

2.环境要求是什么?

3.yii2目录结构是怎样的,每个目录中放了什么文件?

4.怎么配置yii2

5.如何初始化yii2

6.怎么自动生成代码?

7.mvc是什么?

8.怎么写数据库查询语句?

9.怎么配置缓存?

10.i18n怎么配置?

11.怎么安装第三方扩展?

12.怎么卸载第三方扩展?

13.怎么升级yii框架?

1.怎么安装yii2

(1)通过composer安装

ComposerPHP的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。如果你的电脑还没有安装composer,你可以通过下面的命令安装

curl-sS https://getcomposer.org/installer | php mv composer.phar/usr/local/bin/composer
composerself-update
composerglobal require "fxp/composer-asset-plugin:^1.2.0"


接下来执行下面两行命令来安装yii

安装基本版

composercreate-project yiisoft/yii2-app-basic basic 2.0.8

安装高级版

composercreate-project yiisoft/yii2-app-advanced advanced 2.0.8

(2)通过存档文件安装

github下载zip文件,然后解压到web服务器的网页根目录就可以了


2.环境要求是什么?

php版本的要求是需要>=php5.4,更多的请访问框架根目录下面的requirements.php

http://localhost/website/requirements.php


3.yii2目录结构是怎样的,每个目录中放了什么文件?

backend后台目录

common/config公用配置文件目录

common/mail邮件配置

console关于命令行的,我也没有用过

environments/dev开发环境配置

environments/prod生产环境配置

frontend前台目录

tests测试文件,对写好的项目进行测试

vendor第三方类库


4.怎么配置yii2

environments目录进行开发环境和生产环境的配置


5.如何初始化yii2

首先在environments目录中进行配置,然后在根目录下面运行./init

6.怎么自动生成代码?

在前台应用或者后台应用中访问gii控制器


7.mvc是什么?

model:和数据库进行交互,

view:视图,展示页面

controller:控制器将从model中获取的数据发送到相关视图

8.怎么写数据库查询语句?

查询:

// find the customers whose primary key value is 10
$customers = Customer::findAll(10);
$customer = Customer::findOne(10);

// the above code is equivalent to:
$customers = Customer::find()->where(['id' => 10])->all();

// find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findAll([10, 11, 12]);
$customers = Customer::find()->where(['IN','id',[10,11,12]])->all();

// the above code is equivalent to:
$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();

// find customers whose age is 30 and whose status is 1
$customers = Customer::findAll(['age' => 30, 'status' => 1]);

// the above code is equivalent to:
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();

// use params binding
$customers = Customer::find()->where('age=:age AND status=:status')->addParams([':age'=>30,':status'=>1])->all();

// use index
$customers = Customer::find()->indexBy('id')->where(['age' => 30, 'status' => 1])->all();

// get customers count
$count = Customer::find()->where(['age' => 30, 'status' => 1])->count();

// add addition condition
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->andWhere('score > 100')->orderBy('id DESC')->offset(5)->limit(10)->all();

// find by sql
$customers = Customer::findBySql('SELECT * FROM customer WHERE age=30 AND status=1 AND score>100 ORDER BY id DESC LIMIT 5,10')->all();

修改:

// update status for customer-10
$customer = Customer::findOne(10);
$customer->status = 1;
$customer->update();

// the above code is equivalent to:
Customer::updateAll(['status' => 1], 'id = :id',[':id'=>10]);
删除:

// delete customer-10
Customer::findOne(10)->delete();

// the above code is equivalent to:
Customer::deleteAll(['status' => 1], 'id = :id',[':id'=>10]);

9.yii中可以使用什么缓存?

Yii 提供了多种缓存组件以用于保存数据到不同的媒介中。下面是可用缓存组件一览:
yii\caching\ApcCache: 使用PHP APC 扩展。对于集中式应用(如只有一个服务器,没有专门的负载均衡),这被认为是性能最好的缓存选项。 
yii\caching\DbCache: 使用数据库来保存缓存数据。缺省情况下,将在运行时目录下创建和使用一个 SQLite3 数据库。你可以通过设置它的 db 属性来显式指定一个数据库。
yii\caching\DummyCache: 代表一个空缓存。这主要用来简化那些需要检查缓存是否可用的代码。比如在开发过程中或者服务器没有可用的缓存,我们可以使用这个缓存组件。当真实的缓存部署好后,我们可以切换到相应的缓存上。两种情况下,我们可以使用同样的代码:Yii::$app->cache->get($key) 来尝试获取一个数据片段而无须担心 Yii::$app->cache 可能为 null。
yii\caching\FileCache: 文件缓存。这个很适合用来缓存大块数据(比如页面)。
yii\caching\MemCache: 使用PHP memcache 以及 memcached 扩展。对于分布式应用程序(多服务器,有负载均衡),这被认为是性能最好的选项。
yii\redis\Cache: 基于 Redis 键值存储实现了一个缓存组件 (支持 redis 版本 2.6.12 +)。
yii\caching\WinCache: 使用 PHP WinCache(参阅) 扩展。
yii\caching\XCache: 使用 PHP XCache 扩展。
Zend Data Cache 作为底层缓存媒介。



10.i18n怎么配置?

./yii message/config @app/config/i18n.php

设置源代码的路径和翻译文件的路径

<?php
 
return [
    // string, required, root directory of all source files
    'sourcePath' => __DIR__. DIRECTORY_SEPARATOR .'..',
    // Root directory containing message translations.
    'messagePath' => __DIR__ . DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR . 'messages',
    // array, required, list of language codes that the extracted messages
    // should be translated to. For example, ['zh-CN', 'de'].
    'languages' => ['de','es','it','ja'],
    // string, the name of the function for translating messages.
    // Defaults to 'Yii::t'. This is used as a mark to find the messages to be
    // translated. You may use a string for single function name or an array for
    // multiple function names.
    'translator' => 'Yii::t',

生成翻译文件

	
./yii message/extract @app/config/i18n.php


在配置文件中设置默认语言
<?php
 
$params = require(__DIR__ . '/params.php');
 
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language'=>'es', // spanish
    'components' => [

11.怎么安装第三方扩展? 怎么卸载第三方扩展?

composer require 2amigos/yii2-date-time-picker-widget:~1.0
composer remove 2amigos/yii2-date-time-picker-widget

13.怎么升级yii框架?

composer update




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值