Yii2.0 高级版的安装

归档安装: (感觉最简单)

通过归档文件安装 Yii 包括三个步骤:

1.从 - 下载归档文件。

2.将下载的文件解压缩到 Web 目录中。

3.修改 config/web.php 文件,给 cookieValidationKey 配置项添加一个密钥

(若你通过 Composer 安装,则此步骤会自动完成)

如果你是windows系统,请双击advanced目录下的init.bat文件

// !!! 在下面插入一段密钥(若为空) - 以供 cookie validation 的需要
'cookieValidationKey' => '在此处输入你的密钥',

如果你是mac或者linux系统则

cd advanced(你的项目名)
php init

这行命令会提示你选择何种开发环境,并确认是否覆盖当前的配置文件。

下面是输出的内容:

Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0 // 这里选择了 Development 环境

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate yii
   generate common/config/main-local.php
   generate common/config/params-local.php
   generate backend/config/main-local.php
   generate backend/config/params-local.php
   generate backend/web/index.php
   generate backend/web/index-test.php
   generate frontend/config/main-local.php
   generate frontend/config/params-local.php
   generate frontend/web/index.php
   generate frontend/web/index-test.php
   generate console/config/main-local.php
   generate console/config/params-local.php
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii

  ... initialization completed.

新建数据库,修改配置文件

如:新建一个数据库 ##blog##

打开 common/config/main-local.php,修改成

return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=blog',// 自己的数据库
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

再执行

php yii migrate

安装后不能访问问题 顺便美化 url

在forntend/config/main.php urlManager

'urlManager'=> [
            'showScriptName' => false,//隐藏index.php 
            'enablePrettyUrl' => true, 
            'rules' => [
                'index.html'=>'index/main-bg',
                'login.html'=>'***/login',
                'register.html'=>'****/register',
                'introduction/<infos_id:\d+>/<mid:\d+>' =>'modules/introduction', 
            ],
        ],

服务器开启rewrite,aphace在这里介绍,Ngnix 开启 pathinfo 重写即可,具体方法请自行百度

在[Apache安装目录]/conf/httpd.conf中找到

#LoadModule rewrite_module modules/mod_rewrite.so 

去掉#注释

AllowOverride None”改成“AllowOverride All

隐藏index.php,在域名index.php同级目录保存.htaccess

打开编辑器,新建一个空白文档 保存为.htaccess

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php



导航