安装

未匹配的标注

安装

环境要求

PHP >= 5.6.0

使用Composer安装

如果您没有 Composer,请按照文档的 安装Yii 部分中的说明进行安装。

安装 Composer 后,您可以使用以下命令安装应用程序:

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

此命令将高级应用程序模板安装在名为 advanced 的目录中。 如果需要,您可以选择不同的目录名称,来表示您自己的项目。

从归档文件安装(不推荐)

将从 yiiframework.com 下载的归档文件解压缩到直接位于Web根目录下的名为 advanced 的目录。然后按照下一小节中给出的说明进行操作。

准备应用程序

安装应用程序后,必须执行以下步骤来初始化已安装的应用程序。 这些操作仅需执行一次即可。

  1. 打开控制台终端,执行 init 命令并选择 dev 作为环境。
# 命令大纲
/path/to/php-bin/php /path/to/yii-application/init

# 例如
# 进入项目根目录
cd advanced
# php配置过环境变量
php init
# 开启交互模式
0
yes
all

如果,您使用脚本自动化,可以在非交互模式下执行 init 。在此环境下,没有用户交互输入命令执行,所以,直接将参数写在命令语句中。

# env 参数有 Development 与 Production
# overwrite 参数有 All 与 No
/path/to/php-bin/php /path/to/yii-application/init --env=Production --overwrite=All --delete=All
  1. 创建一个新数据库,并相应地调整 common/config/main-local.php 中的 components['db'] 配置,可以参考如下:

    return [
     'components' => [
         'db' => [
              //默认数据库,或主库
             'class' => 'yii\db\Connection',
             'dsn' => 'mysql:host=localhost;port=3306;dbname=demo',
             'username' => 'root',
             'password' => '',
             'charset' => 'utf8mb4',//常用字符集,且支持emoji
             'tablePrefix' => 'demo_',//表前缀
             'attributes' => [
                 PDO::ATTR_STRINGIFY_FETCHES => false, // 提取数据的时候,数值转是否换为字符串
                 PDO::ATTR_EMULATE_PREPARES => false, // 启用或禁用预处理语句的模拟
             ],
    
             // 'enableSchemaCache' => true, // 是否开启缓存, 请了解其中机制在开启,不了解谨慎
             // 'schemaCacheDuration' => 3600, // 缓存时间
             //'schemaCacheExclude' => [],//不应该缓存元数据的表的列表。默认为空数组
    
             // 'enableQueryCache' => true, // 是否启用查询缓存
             // 'queryCacheDuration' => 3600, //查询结果缓存时间
    
             // 一主多从,从库的通用配置
             /*'slaveConfig' => [
                 'username' => 'root',
                 'password' => '',
                 'charset' => 'utf8mb4',//常用字符集,且支持emoji
                 'tablePrefix' => 'demo_',//表前缀
                 'attributes' => [
                     PDO::ATTR_STRINGIFY_FETCHES => false, // 提取数据的时候,数值转是否换为字符串
                     PDO::ATTR_EMULATE_PREPARES => false, // 启用或禁用预处理语句的模拟
    
                     // 使用一个更小的连接超时
                     PDO::ATTR_TIMEOUT => 10,
                 ],
             ],*/
    
             // 从库的配置列表
             /*'slaves' => [
                 ['dsn' => 'mysql:host=localhost;port=3306;dbname=demo1'],
                 ['dsn' => 'mysql:host=localhost;port=3306;dbname=demo2'],
             ],*/
    
             /*'on afterOpen' => function($event) {
                 // $event->sender refers to the DB connection
                 $event->sender->createCommand("SET time_zone = 'UTC'")->execute();
             }*/
         ],
     ]
    ];
  2. 打开控制台终端,执行数据库迁移命令(项目模板中有自带的示例的迁移脚本,通常在 /console/migrations/ 目录下):

    # 命令大纲
    /path/to/php-bin/php /path/to/yii-application/yii migrate
    # 例如
    # 通常在项目跟目录下执行,即可跟踪显示迁移信息
    php yii migrate
  3. 设置 Web 服务器的文档根目录:

    • 对于前台 /path/to/yii-application/frontend/web/ 并且使用URL http://frontend.test/
    • 对于后台 /path/to/yii-application/backend/web/ 并且使用URL http://backend.test/

对于Apache,使用如下配置:

       <VirtualHost *:80>
           ServerName frontend.test
           DocumentRoot "/path/to/yii-application/frontend/web/"

           <Directory "/path/to/yii-application/frontend/web/">
               # use mod_rewrite for pretty URL support
               RewriteEngine on
               # If a directory or a file exists, use the request directly
               RewriteCond %{REQUEST_FILENAME} !-f
               RewriteCond %{REQUEST_FILENAME} !-d
               # Otherwise forward the request to index.php
               RewriteRule . index.php

               # use index.php as index file
               DirectoryIndex index.php

               # ...other settings...
           </Directory>
       </VirtualHost>

       <VirtualHost *:80>
           ServerName backend.test
           DocumentRoot "/path/to/yii-application/backend/web/"

           <Directory "/path/to/yii-application/backend/web/">
               # use mod_rewrite for pretty URL support
               RewriteEngine on
               # If a directory or a file exists, use the request directly
               RewriteCond %{REQUEST_FILENAME} !-f
               RewriteCond %{REQUEST_FILENAME} !-d
               # Otherwise forward the request to index.php
               RewriteRule . index.php

               # use index.php as index file
               DirectoryIndex index.php

               # ...other settings...
           </Directory>
       </VirtualHost>

nginx使用如下配置:

       server {
           charset utf-8;
           client_max_body_size 128M;

           listen 80; ## listen for ipv4
           #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

           server_name frontend.test;
           root        /path/to/yii-application/frontend/web/;
           index       index.php;

           access_log  /path/to/yii-application/log/frontend-access.log;
           error_log   /path/to/yii-application/log/frontend-error.log;

           location / {
               # Redirect everything that isn't a real file to index.php
               try_files $uri $uri/ /index.php$is_args$args;
           }

           # uncomment to avoid processing of calls to non-existing static files by Yii
           #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
           #    try_files $uri =404;
           #}
           #error_page 404 /404.html;

           # deny accessing php files for the /assets directory
           location ~ ^/assets/.*\.php$ {
               deny all;
           }

           location ~ \.php$ {
               include fastcgi_params;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_pass 127.0.0.1:9000;
               #fastcgi_pass unix:/var/run/php5-fpm.sock;
               try_files $uri =404;
           }

           location ~* /\. {
               deny all;
           }
       }

       server {
           charset utf-8;
           client_max_body_size 128M;

           listen 80; ## listen for ipv4
           #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

           server_name backend.test;
           root        /path/to/yii-application/backend/web/;
           index       index.php;

           access_log  /path/to/yii-application/log/backend-access.log;
           error_log   /path/to/yii-application/log/backend-error.log;

           location / {
               # Redirect everything that isn't a real file to index.php
               try_files $uri $uri/ /index.php$is_args$args;
           }

           # uncomment to avoid processing of calls to non-existing static files by Yii
           #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
           #    try_files $uri =404;
           #}
           #error_page 404 /404.html;

           # deny accessing php files for the /assets directory
           location ~ ^/assets/.*\.php$ {
               deny all;
           }

           location ~ \.php$ {
               include fastcgi_params;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_pass 127.0.0.1:9000;
               #fastcgi_pass unix:/var/run/php5-fpm.sock;
               try_files $uri =404;
           }

           location ~* /\. {
               deny all;
           }
       }
  1. 更改主机文件以将域指向您的服务器。

    • Windows: c:\Windows\System32\Drivers\etc\hosts
    • Linux: /etc/hosts

添加以下行:

127.0.0.1   frontend.test
127.0.0.1   backend.test
  1. 打开您的浏览器,并访问 frontend.test/

  2. 通过选择前端主页顶部的 Sign Up 菜单选项创建一个用户。

  3. 提供所请求的凭据,并使用 Signup 按钮完成数据输入。你会收到一条信息:

# 谢谢您的注册,请检查您的收件箱,并确认电子邮件
Thank you for registration. Please check your inbox for verification email.
  1. 尽管已经发送了确认邮件,但该邮件器的默认设置仍然会阻止真实邮件的发送。相反,在 @frontend/runtime/mail 目录中创建一个 eml 格式的文件。可以使用 Outlook 或 Thunderbird 等邮件客户机打开该文件,也可以使用文本编辑器检索用于确认用户创建的 URL。在将该 URL 粘贴到浏览器之前,需要修改该 URL 以删除 引用的可打印编码

    这可以手动完成,具体如下:

    • 删除软换行符 ‘=’ 和换行符,以创建下面的单行
    • 修改 ‘=3D’ 为 ‘=’
    • 在 Mac / Linux 上,将 \r\n 转换为 \n - MIME CRLF 换行符是 “真实的”,应该保留。

    将此URL粘贴到浏览器选项卡中,以完成User的创建。你会看到这个信息:

Your email has been confirmed!
  1. 现在,您将自动登录到前端应用程序。然后可以使用相同的凭据登录到后端应用程序。

注意:如果要在单个域上运行高级模板,则 / 是前端,而 /admin 是后端,请参阅 在共享主机上使用高级项目模板

使用 Vagrant 安装(不推荐)

这是最简单的安装方式,但是耗时较长(可能约 20 分钟)。

这种安装方式不需要预先安装的软件(如Web服务器,PHP,MySQL等) - 只是做下一步!

Linux/Unix 用户手册

  1. 安装 VirtualBox
  2. 安装 Vagrant
  3. 创建 GitHub personal API token
  4. 准备项目:
git clone https://github.com/yiisoft/yii2-app-advanced.git
cd yii2-app-advanced/vagrant/config
cp vagrant-local.example.yml vagrant-local.yml
  1. 将您的GitHub个人API令牌放置到 vagrant-local.yml
  2. 将目录更改为项目根目录:
cd yii2-app-advanced
  1. 执行如下命令:
vagrant plugin install vagrant-hostmanager
vagrant up

等待完成后,在浏览器中访问如下URL即可

Windows 用户手册

  1. 安装 VirtualBox

  2. 安装 Vagrant

  3. 重启电脑

  4. 创建 GitHub personal API token

  5. 准备项目:

    • 下载 yii2-app-advanced
    • 解压
    • 进入 yii2-app-advanced-master/vagrant/config 文件夹
    • 重命名 vagrant-local.example.ymlvagrant-local.yml
  6. 将您的GitHub个人API令牌放置到 vagrant-local.yml

  7. 添加如下代码到 hosts 文件:

   192.168.83.137 y2aa-frontend.test
   192.168.83.137 y2aa-backend.test
  1. 打开终端 (cmd.exe), 切换路径至项目根目录 并且执行如下命令:
   vagrant plugin install vagrant-hostmanager
   vagrant up

(猛击 这里 查看如何在命令提示符中更改目录)

等待完成后,在浏览器中访问如下URL即可

配置 Composer

安装项目模板后,最好调整默认的 composer.json ,它可以在根目录下找到:

{
    "name": "yiisoft/yii2-app-advanced",
    "description": "Yii 2 Advanced Project Template",
    "keywords": ["yii2", "framework", "advanced", "project template"],
    "homepage": "http://www.yiiframework.com/",
    "type": "project",
    "license": "BSD-3-Clause",
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.6.0",
        "yiisoft/yii2": "~2.0.14",
        "yiisoft/yii2-bootstrap4": "~2.0.0",
        "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0"
    },
    "require-dev": {
        "yiisoft/yii2-debug": "~2.1.0",
        "yiisoft/yii2-gii": "~2.2.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/codeception": "^4.0",
        "codeception/module-asserts": "^1.0",
        "codeception/module-yii2": "^1.0",
        "codeception/module-filesystem": "^1.0",
        "phpunit/phpunit": "~5.7.27 || ~6.5.5",
        "codeception/verify": "~0.5.0 || ~1.1.0",
        "symfony/browser-kit": ">=2.7 <=4.2.4"
    },
    "config": {
        "process-timeout": 1800,
        "fxp-asset": {
            "enabled": false
        }
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://asset-packagist.org"
        }
    ]
}

首先,我们要更新基本信息。 更改 namedescriptionkeywordshomepagesupport 来匹配您的项目。

接下来是见证奇迹的时刻. 您可以将您的应用程序需要的更多包添加到 require 部分。所有这些包都来自 packagist.org 浏览这里你可以找到更多的实用的免费代码。

在你的 composer.json 改变之后,你可以运行 composer update --prefer-dist ,等待程序包下载完成,安装后,就可以使用它们了。 包里面所有的类都会自动加载。

💖喜欢本文档的,欢迎点赞、收藏、留言或转发,谢谢支持!
作者邮箱:zhuzixian520@126.com,github地址:github.com/zhuzixian520

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
zhuzixian520
讨论数量: 0
发起讨论 只看当前版本


暂无话题~