PHPackages                             hubeiwei/hello-yii2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. hubeiwei/hello-yii2

ActiveProject[Framework](/categories/framework)

hubeiwei/hello-yii2
===================

练手用

1606PHP

Since Dec 29Pushed 7y ago1 watchersCompare

[ Source](https://github.com/hubeiwei/hello-yii2)[ Packagist](https://packagist.org/packages/hubeiwei/hello-yii2)[ RSS](/packages/hubeiwei-hello-yii2/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

目录
--

[](#目录)

- [说明](#%E8%AF%B4%E6%98%8E)
- [项目部署](#%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2)

    - [PHP配置](#php%E9%85%8D%E7%BD%AE)
    - [安装第三方扩展和创建数据库表](#%E5%AE%89%E8%A3%85%E7%AC%AC%E4%B8%89%E6%96%B9%E6%89%A9%E5%B1%95%E5%92%8C%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8)
    - [服务器配置](#%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%85%8D%E7%BD%AE)
    - [使用](#%E4%BD%BF%E7%94%A8)
- [结构说明](#%E7%BB%93%E6%9E%84%E8%AF%B4%E6%98%8E)

    - [文件和目录](#%E6%96%87%E4%BB%B6%E5%92%8C%E7%9B%AE%E5%BD%95)
    - [Model](#model)
    - [Layout](#layout)
- [打赏](#%E6%89%93%E8%B5%8F)

说明
--

[](#说明)

这是我2016年在空闲时间用 yii2-basic 模板来练手的东西，里面的代码不一定能在实际工作中使用，也不一定是最好的写法，而且年代久远，仅供参考，我只会时不时上来更新一下 composer 测试一下有没有问题再去公司的项目里更新，新功能估计不会再搞。

目前可能是你能学到一点破姿势的地方：

前台文章首页（/frontend/article/index）：使用 ActiveDataProvider 遍历数据、分页、排序（虽然很基础，但比起传统的手动加 offset、limit、order by 还得算总数方便多了）。

个人中心（/user/home）：这里的页面加载你看起来感觉会很快，因为我用了 pjax，不过目前有个 bug，点击链接时我会把链接的文字加到右边面板上，但是后退的时候就尴尬了，我现在有两个方案，但还懒得去改。

另外，有些东西我封装到[这个项目](https://github.com/hubeiwei/yii2-tools)里了，具体作用去看说明和我给出的 demo 吧。

项目部署
----

[](#项目部署)

### PHP配置

[](#php配置)

扩展：openssl、pdo\_mysql、fileinfo

程序上的上传文件大小限制在 `app\models\Music::MUSIC_SIZE`，我随便设置了一个 20MB，php 的 `post_max_size` 和 `upload_max_filesize` 两个值需要配置一下。

### 安装 composer 依赖（vendor 目录）

[](#安装-composer-依赖vendor-目录)

首先你要有 [composer](http://docs.phpcomposer.com/)，然后按顺序执行以下命令，如果你觉得速度慢的话，可以参考我的[这篇文章](http://laohu321.cc/2017/01/terminal-accelerate)。

```
composer self-update
composer install --prefer-dist

```

### 生成本地配置文件

[](#生成本地配置文件)

```
php init --env=Development --overwrite=all

```

### 创建数据库表

[](#创建数据库表)

在 MySql 创建一个 utf8 数据库，在 config/db.php 文件配置好之后，按顺序执行以下命令：

```
php yii migrate

```

### 服务器配置

[](#服务器配置)

直接参考[官方文档](https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/start-installation.md#%E9%85%8D%E7%BD%AE-web-%E6%9C%8D%E5%8A%A1%E5%99%A8-)，我的 nginx 配置就是直接抄官方文档里面的。

nginx 也看看[这篇文章](http://www.getyii.com/topic/31)。

### 使用

[](#使用)

在 config/components-local.php 里找到邮箱的配置，配置好了才能用找回密码的功能。

已经生成好的用户名和密码如下：

身份用户名密码超级管理员adminasdf1234普通用户testasdf1234结构说明
----

[](#结构说明)

完成上面的步骤后你就可以使用了，如果你想了解我的项目的话，可以看看这一节。

我的代码是从 gii 生成的 model 和 CRUD 代码上修改而来的，如果你也是这个套路的话，那我的代码应该还算容易看懂，就 model 和原来的有些区别，详情请[往下拉](#model)。

### 文件和目录

[](#文件和目录)

没列出的文件随便看看就好。

```
common                    一些我自己封装的代码和改写的类

config
├ components.php          组件配置
├ components-local.php    本地组件配置，在 environments 里通过 init 生成
├ db.php                  数据库配置，在 environments 里通过 init 生成
├ modules.php             模块配置
└ web.php                 框架配置

models                    放 model 的地方，下面有详细说明

modules
├ backend                 后台模块
├ frontend                前台模块
└ user                    用户相关，例如登入登出、个人中心、找回密码等

views                     放布局文件和错误页面用，下面有详细说明

```

### models 目录

[](#models-目录)

我用 gii 生成 model 到 models/base 目录下，取名为 'ModelBase'，然后在 models 目录下新建一个 'Model' 来继承 'ModelBase'，以后只编辑 'Model'，这样做的好处是重新生成 model 可以直接覆盖。

用 gii 生成 crud 代码，其中有一项 “Search Model Class”，我把它生成到 models/search 目录下。

### views/layout 目录

[](#viewslayout-目录)

布局结构和说明：

```
base_html5.php         最外层，最基础的 html 结构，所有人都能用，layout 不直接指向这里
└ master.php           根据自己项目自定义的最外层，layout 不直接指向这里
  ├ frontend.php       前台外层，layout 不直接指向这里
  │ ├ main.php         前台，框架默认指向的地方
  │ ├ user.php         用户模块
  │ └ user_form.php    用户模块的表单
  └ backend.php        后台

```

layout 会统一在 config/modules.php 里配置，frontend 模块里两个控制器的 `beforeAction()` 方法里也有修改布局的代码。

只做输出用的文件，在同级的 include 目录下：

```
layout/frontend.php
└ include/frontend_nav.php    顶部 bootstrap 的菜单

layout/backend.php
├ include/backend_menu.php    左侧菜单
└ include/bacnend_nav.php     顶部 bootstrap 的菜单

```

打赏
--

[](#打赏)

如果觉得我做的东西对你有帮助的话，求打赏一杯 coffee，这样我会有更多动力去分享更多 yii2 的内容。

[![支付宝](https://raw.githubusercontent.com/hubeiwei/hubeiwei.github.io/master/images/pay/ali_pay.jpg)](https://raw.githubusercontent.com/hubeiwei/hubeiwei.github.io/master/images/pay/ali_pay.jpg)

[![微信](https://raw.githubusercontent.com/hubeiwei/hubeiwei.github.io/master/images/pay/wechat_pay.png)](https://raw.githubusercontent.com/hubeiwei/hubeiwei.github.io/master/images/pay/wechat_pay.png)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 99.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/621e9712a2d6e204da46c1a31d9025f3a804d225ee054701cffdb408e425000a?d=identicon)[hubeiwei](/maintainers/hubeiwei)

---

Top Contributors

[![hubeiwei](https://avatars.githubusercontent.com/u/17850096?v=4)](https://github.com/hubeiwei "hubeiwei (396 commits)")[![ciccio](https://avatars.githubusercontent.com/u/59334?v=4)](https://github.com/ciccio "ciccio (1 commits)")

### Embed Badge

![Health badge](/badges/hubeiwei-hello-yii2/health.svg)

```
[![Health](https://phpackages.com/badges/hubeiwei-hello-yii2/health.svg)](https://phpackages.com/packages/hubeiwei-hello-yii2)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
