ardhiandharma/yii2-basic

Yii 2 Basic 项目模板 plus

v2020.10.31 2020-10-30 22:20 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:46 UTC


README

Yii 2 Basic 项目模板


Yii 2 Basic 项目模板是一个骨架 Yii 2 应用,非常适合快速创建小型项目。

该模板包含基本功能,包括用户登录/登出和联系页面。它包含所有常用的配置,使您能够专注于添加新功能到您的应用。

Latest Stable Version Total Downloads ci-linux ci-windows

目录结构

  assets/             contains assets definition
  commands/           contains console commands (controllers)
  config/             contains application configurations
  controllers/        contains Web controller classes
  mail/               contains view files for e-mails
  models/             contains model classes
  runtime/            contains files generated during runtime
  tests/              contains various tests for the basic application
  vendor/             contains dependent 3rd-party packages
  views/              contains view files for the Web application
  web/                contains the entry script and Web resources

要求

此项目模板的最低要求是您的 Web 服务器支持 PHP 5.6.0。

安装

通过 Composer 安装

如果您没有 Composer,您可以通过访问 getcomposer.org 上的说明进行安装。

然后,您可以使用以下命令安装此项目模板

composer create-project --prefer-dist yiisoft/yii2-app-basic basic

现在,您应该可以通过以下 URL 访问应用程序,假设 basic 是 Web 根目录下的直接目录。

https:///basic/web/

从存档文件安装

将从 yiiframework.com 下载的存档文件解压缩到 Web 根目录下名为 basic 的目录中。

config/web.php 文件中将 cookie 验证密钥设置为某个随机秘密字符串

'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => '<secret random string goes here>',
],

然后您可以通过以下 URL 访问应用程序

https:///basic/web/

使用 Docker 安装

更新供应商包

docker-compose run --rm php composer update --prefer-dist

运行安装触发器(创建 cookie 验证代码)

docker-compose run --rm php composer install    

启动容器

docker-compose up -d

然后您可以通过以下 URL 访问应用程序

http://127.0.0.1:8000

注意

  • 最低要求 Docker 引擎版本 17.04 用于开发(见 调整卷挂载的性能
  • 默认配置使用您家目录中的主机卷 .docker-composer 作为 composer 缓存

配置

数据库

使用真实数据编辑文件 config/db.php,例如

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=yii2basic',
    'username' => 'root',
    'password' => '1234',
    'charset' => 'utf8',
];

注意

  • Yii 不会为您创建数据库,您需要手动创建数据库才能访问它。
  • 检查并编辑 config/ 目录中的其他文件以按需自定义您的应用程序。
  • 有关基本应用程序测试的特定信息,请参阅 tests 目录中的 README。

测试

测试位于 tests 目录。它们是用 Codeception PHP 测试框架 开发的。默认情况下,有 3 个测试套件

  • 单元
  • 功能
  • 接受

可以通过运行以下命令执行测试

vendor/bin/codecept run

上面的命令将执行单元测试和功能测试。单元测试是测试系统组件,而功能测试是测试用户交互。接受测试默认禁用,因为它们需要额外的设置,因为它们在真实浏览器中执行测试。

运行接受测试

要执行接受测试,请执行以下操作

  1. tests/acceptance.suite.yml.example 重命名为 tests/acceptance.suite.yml 以启用套件配置

  2. composer.json 中的 codeception/base 包替换为 codeception/codeception 以安装功能齐全的 Codeception 版本

  3. 使用 Composer 更新依赖项

     composer update  
    
  4. 下载并启动 Selenium 服务器

     java -jar ~/selenium-server-standalone-x.xx.x.jar
    

    如果您从版本 48 开始使用 Firefox 浏览器或从版本 53 开始使用 Google Chrome 浏览器与 Selenium 服务器 3.0 一起使用,您必须下载 GeckoDriverChromeDriver 并用它们启动 Selenium

     # for Firefox
     java -jar -Dwebdriver.gecko.driver=~/geckodriver ~/selenium-server-standalone-3.xx.x.jar
        
     # for Google Chrome
     java -jar -Dwebdriver.chrome.driver=~/chromedriver ~/selenium-server-standalone-3.xx.x.jar
    

    作为另一种方法,您可以使用已经配置好的 Docker 容器,使用较旧的 Selenium 和 Firefox 版本

     docker run --net=host selenium/standalone-firefox:2.53.0
    
  5. (可选) 创建 yii2basic_test 数据库,如果您有迁移,请应用它们。

    tests/bin/yii migrate
    

    数据库配置可以在 config/test_db.php 文件中找到。

  1. 启动 web 服务器

     tests/bin/yii serve
    
  2. 现在您可以运行所有可用的测试

    # run all available tests
    vendor/bin/codecept run
    
    # run acceptance tests
    vendor/bin/codecept run acceptance
    
    # run only unit and functional tests
    vendor/bin/codecept run unit,functional
    

代码覆盖率支持

默认情况下,在 codeception.yml 配置文件中禁用了代码覆盖率,您应该取消注释所需的行以收集代码覆盖率。您可以使用以下命令运行测试并收集覆盖率

#collect coverage for all tests
vendor/bin/codecept run --coverage --coverage-html --coverage-xml

#collect coverage only for unit tests
vendor/bin/codecept run unit --coverage --coverage-html --coverage-xml

#collect coverage for unit and functional tests
vendor/bin/codecept run functional,unit --coverage --coverage-html --coverage-xml

您可以在 tests/_output 目录下查看代码覆盖率输出。

迁移

请执行此迁移

  1. php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations
  2. php yii migrate --migrationPath=@mdm/admin/migrations
  3. php yii migrate --migrationPath=@yii/rbac/migrations
  4. php yii migrate

附加信息

  1. 默认账户用户名:administrator,密码:123456
  2. 有关用户管理,请参阅 https://github.com/dektrium/yii2-user/blob/master/docs/README.md
  3. 有关 RBAC,请参阅 https://github.com/mdmsoft/yii2-admin