kartik-v/yii2-app-practical-b

Yii 2 Practical-B 应用程序模板

v1.6.3 2017-06-04 16:49 UTC

This package is auto-updated.

Last update: 2024-09-06 09:07:27 UTC


README

Krajee Logo
Yii 2 Practical-B 项目模板 Donate

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

Yii 2 Practical-B 应用程序模板是基于 yii2-basic 模板 的一个骨架,非常适合快速创建小型项目。该模板允许通过直接从应用程序根目录访问应用程序的实用方法。

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

为什么选择 yii2-practical-b?

安装 app 后,在 yii2-basic 应用程序中,您通常通过以下方式访问前端:

http://domain/app/web

然而,在许多实际场景(尤其是在共享和单域主机上)中,人们希望他们的用户直接以以下方式访问应用程序:

http://domain/app

yii2-app-practical-b 允许您通过仔细移动和重新排列前端的开机文件和组件来实现这一点,使其能够直接从应用程序根目录工作。完全消除了 web 文件夹,并且可以通过这种方式直接访问应用程序前端。

http://domain/app

应用程序配置的其他方面与 yii2-basic 应用程序保持不变。应用程序根目录中的原始 assets 文件夹重命名为 assets_b,而 web/assets 文件夹则移动到应用程序根目录。

一些关键新增功能

  1. 该模板为具有 Apache 网络服务器的用户预配置了某些安全设置。它已设置默认的 .htaccess 安全配置。
  2. 该模板默认启用了 prettyUrl,并对 .htaccess 以及配置目录中的 urlManager 组件配置进行了更改。

目录结构

    /                   contains the entry script and web resources
    assets/             contains the web runtime assets
    assets_b/           contains application assets such as JavaScript and CSS
    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 yii2-practical-b application
    vendor/             contains dependent 3rd-party packages
    views/              contains view files for the Web application

需求

此项目模板的最小需求是您的 Web 服务器支持 PHP 5.4.0。

安装

通过 Composer 安装

如果您没有 Composer,您可以按照 getcomposer.org 上的说明进行安装。

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

php composer.phar global require "fxp/composer-asset-plugin:^1.3.1"
php composer.phar create-project --prefer-dist --stability=dev kartik-v/yii2-app-practical-b practical-b

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

http://localhost/practical-b

从存档文件安装

将从 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 访问应用程序

http://localhost/practical-b/

配置

数据库

使用真实数据编辑文件 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
    

    如果使用 Selenium 服务器 3.0 与 Firefox 浏览器自 v48 或 Google Chrome 自 v53,您必须下载 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
    

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

    docker run --net=host selenium/standalone-firefox:2.53.0
    
  5. (可选) 创建 yii2_basic_tests 数据库,并根据需要应用迁移进行更新。

    tests/bin/yii migrate
    

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

  6. 启动 web 服务器

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

    # 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-html --coverage-xml

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

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

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